Python Project for Kids: Python Age Calculator

Table Of Content:

Introduction: 

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. 

python game for kids

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


Pseudo Code:

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 exit():

   Close window

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


python game for kids

Main Steps:

This project can be broken down into 4 main steps: 

  1. Import libraries and set up the window
  2. Create the GUI labels
  3. Create the Exit and GetAge functions
  4. 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:

from datetime import date
import tkinter as tk

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:

root = tk.Tk()
root.geometry("400x300")
root.title('Geekedu Age Calculator')
today = date.today()

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)
 title = tk.Label(root,text="The Age Calculator!",font=("Arial", 20),fg="black")
instructions = tk.Label(root,font=("Arial",12),text="Enter a date below:",fg="black")
day=tk.Label(root,text="Date: ",font=('Arial',12,"bold"),fg="darkgreen")
month=tk.Label(root,text="Month: ",font=('Arial',12,"bold"),fg="darkgreen")
year=tk.Label(root,text="Year: ",font=('Arial',12,"bold"),fg="darkgreen")
dayInput=tk.Entry(root,width=5)
monthInput=tk.Entry(root,width=5)
yearInput=tk.Entry(root,width=5)
calculatedAge = tk.Label(root,text="Calculated Age:",font=('Arial',12,"bold"),fg="darkgreen")
result=tk.Text(root,width=5,height=0,state="disabled")

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:

title.place(x=70,y=5)
instructions.place(x=100,y=40)
day.place(x=100,y=70)
month.place(x=100,y=95)
year.place(x=100,y=120)
dayInput.place(x=180,y=70)
monthInput.place(x=180,y=95)
yearInput.place(x=180,y=120)
calculatedAge.place(x=60,y=200)
result.place(x=220,y=203)

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:

def exit():
    root.destroy()

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:

def getAge():
    d= int(dayInput.get())
    m=int(monthInput.get())
    y=int(yearInput.get())
    age =today.year-y-((today.month, today.day)<(m,d))
    result.config(state='normal')
    result.delete('1.0', tk.END)
    result.insert(tk.END,age)
    result.config(state='disabled')

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!

python game for kids

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.

calculateAge=tk.Button(root,text="Calculate Age!",font=("Arial",13),command=getAge)
exit=tk.Button(root,text="Exit",font=("Arial",13),command=exit)
calculateAge.place(x=100,y=150)
exit.place(x=150,y=230)
 
root.mainloop()

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.

Project Complete!

python game for kids

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.

Geek Team

Geekedu is an expert in Coding and Math learning. Our goal is to inspire and empower youth to use their knowledge of technology to become the influencers, inventors and innovators of the future.

Sign up and get a 60-minute free assessment class

Book A FREE Trial