In this article, we are going to be using Python and the Tkinter library to create a simple age calculator program that will allow users to input a date using the day, month, and year, and the program will let the user know how long it has been since that day. This project is great for young kids who may not be able to make these calculations manually and is also the perfect way to improve your coding skills.
This project is a great way to learn more about making programs by creating a useful program that can be used for a wide variety of purposes. When learning to code, many people tend to focus on learning abstract concepts, but it’s putting them into practice when making projects that really help kids learn to code well and efficiently. That’s why this age calculator is a great way to build both your creative thinking and problem-solving skills while making a functional computer program.
Who is this Project For?
This is a beginner-level project for those who are new to Python. Before starting this project, you should already have experience with creating simple Tkinter GUIs (Graphical User Interfaces), and creating functions. These are the concepts that you will use and build upon as you code this project.
What Will We Learn?
This program focuses on creating a GUI using the Tkinter library, which is used to display the print program. It also uses the DateTime library to calculate the current date, and a getAge() function is used to calculate the time since the given date. These concepts are more advanced but are important to understand, making this the perfect program to improve these essential skills.
Features to Consider:
- Users must enter their chosen date using date, month, year format
- When the ‘calculate age’ button is pressed, the program will run
- If the user presses the exit button, the program will stop
Included below is the pseudocode for this age calculator program:
Import Tkinter and datetime libraries
Setup window as root
Set window size to 400x300 px and window title to “Geekedu Age Calculator”
Create a variable to store today’s date
Create a title called “The Age Calculator!” with font Arial, 20px
Create a label called “Enter a date below:” with font Arial, 12 px
Create three labels called Date, Month, and Year, all in Arial, 12px
Create 3 entry boxes for day, month, and year inputs with width of 5
Create a label “Calculated Age:” font Arial 12 px
Create a “Result” text with a width of 5
Place title at (70, 5)
Place instructions at (100, 40)
Place day at (100, 70)
Place month at (100, 95)
Place year at (100, 120)
Place dayInput at (180, 70)
Place monthInput at (180, 95)
Place yearInput at (180, 120)
Place calculatedAge at (60, 200)
Place result at (200, 203)
function buttonPressed(self, someButton, eraser_mode=False):
Take input from the dateTime library for the for day, month, and year
age =today’s year- given year -((today’s month , today’s day)<(given month, given day))
Set result state to normal
Set state to disabled
Create a button called “Calculate age!” with the command getAge
Create a button called “Exit” with the command exit
Place calculateAge at (100, 150)
Place exit at (150, 230)
Run the mainloop
This project can be broken down into 4 main steps:
- Import libraries and set up the window
- Create the GUI labels
- Create the Exit and GetAge functions
- Create the buttons
Step 1: Import Libraries and Set Up The Window
The first thing we’ll need to do when creating this game is import all of the required libraries. For this program, we will be using the Tkinter library to create the GUI as well as the DateTime library to find today’s date. This will be used in the calculations when the program calculates the time difference between the two dates. We can import these libraries using this code:
Next, we need to set up the Tkinter window. We will initialise Tkinter, select dimensions for the window, and set a title. We will also create a variable called “today” and use it to store today’s date from the DateTime library. Once this has been done, the code is:
Step 2: Create the GUI Labels
Now it’s time to create the labels and text boxes for the GUI. These will include the program instructions, labels for the date, month, and year of the desired date, and boxes to input these values. The code for this section may seem long at first, but each label follows a very similar code format, so once you learn how to make one, it’s easy to make the others.
Hint: The format for a label is as follows: labelName = tk.Label(root,text="Insert Text Here",font=(“font”, size, style(optional) ),fg="colourName or hex code")
Hint: The format for a text input is as follows: inputName=tk.Entry(root,width=width)
Now that the labels have been created, it’s time to set the coordinates for each object. We will do this using the .place() method, and the code is as follows:
Step 3: Create the Exit and GetAge Functions
Now that we have finished creating the main GUI setup, it’s time to create the rest of the functions. We will start by creating the exit function. This is a very short two-line function that will exit the program and close the window whenever the exit button is pressed. The code for this section will look like this:
Next, it’s time to create the getAge function. This function will essentially collect the date information that the user provided and compare it to today’s date using the dateTime library. Once this is complete, the code should be:
As you can see, this function consists mostly of defining the variables, then subtracting values to find the resulting time between the two dates. Once this function is complete, we’re on to the next and final step for the project!
Step 4: Create the Buttons
Finally, we’re on to the last step, which will include creating the calculate age and exit buttons to run the functions we just created in step 3.
You’ll notice that each of the buttons follows a very similar format to the labels and text boxes we created earlier. We have also used the .place() method again to position each button. Finally, we called root.mainloop() to run the entire program from the beginning.
Now the project is complete! We hope you’ve had fun creating this simple age calculator, and hopefully, you’ve learned more about programming with Python! Feel free to experiment with the code as well, try to customize it to how you like it, or try to add additional features such as being more specific with dates (such as specifying how many days or months it has been)!
Make sure to test your code now and see how it works. If you’re stuck or have any issues with your code, try reviewing it again either in your text editor or by looking at the code included in the article as a reference.