Python Tkinter: GUI Age Calculator

Hello there learner! Today we are going to build a GUI application, Age Calculator using Python Tkinter.

Let us begin!

The name of the application says everything that the application will be doing so let’s get right to building the application.

Designing the Age Calculator Interface

The first step in any Tkinter project is designing of the application window. The designing includes two steps namely:

  1. Creating a custom window
  2. Adding Elements to the window application

Creating a custom window

To create a empty custom window. We have to import the tkinter module and create a window object. We then add the background color and title of the window application.

And we also set the resizable features to False to make sure the dimensions remain unchanged. The code for the same is shown below.

import tkinter as tk
window = tk.Tk()
window.geometry("400x300")
window.config(bg="#F7DC6F")
window.resizable(width=False,height=False)
window.title('Age Calculator!')

Adding all necessary elements to the window

The next step involves adding all the labels, entry boxes, buttons and text boxes in our application.

1. Labels

We would be using multiple labels where each label would serve a different purpose. We would have labels for the introduction information, the labeling of the entry boxes asking for the date of birth from the user.

2. Entry Boxes

We will be using three entry boxes for the date of birth of the user. One will be for the date, one for the month while the last one would be for the year of birth.

3. Buttons

In our application, we will be using two buttons, one for calculating age and the other to exit the application.

4. TextBoxes

We will be using only one textbox to display the calculated age.

The whole design code is shown below. we will be customizing the elements according to our preference. You can change it if you like to.

l1 = tk.Label(window,text="The Age Calculator!",font=("Arial", 20),fg="black",bg="#F7DC6F")
l2 = tk.Label(window,font=("Arial",12),text="Enter your birthday which includes the day-month-year.",fg="black",bg="#F7DC6F")

l_d=tk.Label(window,text="Date: ",font=('Arial',12,"bold"),fg="darkgreen",bg="#F7DC6F")
l_m=tk.Label(window,text="Month: ",font=('Arial',12,"bold"),fg="darkgreen",bg="#F7DC6F")
l_y=tk.Label(window,text="Year: ",font=('Arial',12,"bold"),fg="darkgreen",bg="#F7DC6F")
e1=tk.Entry(window,width=5)
e2=tk.Entry(window,width=5)
e3=tk.Entry(window,width=5)

b1=tk.Button(window,text="Calculate Age!",font=("Arial",13),command=get_age)

l3 = tk.Label(window,text="The Calculated Age is: ",font=('Arial',12,"bold"),fg="darkgreen",bg="#F7DC6F")
t1=tk.Text(window,width=5,height=0,state="disabled")

b2=tk.Button(window,text="Exit Application!",font=("Arial",13),command=exit)

Placing the elements on the screen

To place the elements on the screen, we make use of the place function which requires x and y coordinates for the element to place the items at the correct position.

The code to place the elements is shown below:

l1.place(x=70,y=5)
l2.place(x=10,y=40)
l_d.place(x=100,y=70)
l_m.place(x=100,y=95)
l_y.place(x=100,y=120)
e1.place(x=180,y=70)
e2.place(x=180,y=95)
e3.place(x=180,y=120)
b1.place(x=100,y=150)
l3.place(x=50,y=200)
t1.place(x=240,y=203)
b2.place(x=100,y=230)

Interface for the Age Calculator in Tkinter

The final design of the application looks like the window displayed below.

Initial State Age Calculator
Initial State Age Calculator

Adding Functionalities to the Buttons

1. Calculate Age Button

To calculate the age we first have to get the three inputs ( data – month – year ) from the three entry boxes. Now the next step involves calculating the difference between the date of birth and the current date.

To get the current date we import date function from datetime module. We also create a object which stores today’s whole date. The code for the same is shown below:

from datetime import date
today = date.today()

Now we create the function which calculates the age and is connected to the calculate age button. The function gets the three entries and finds the age ( difference between current and date of birth )

The age calculated is then inserted into the text box after clearing the previous info in the textbox. The code for the same is shown below:

def get_age():
    d= int(e1.get())
    m=int(e2.get())
    y=int(e3.get())
    age = today.year-y-((today.month, today.day)<(m,d))
    t1.config(state='normal')
    t1.delete('1.0', tk.END)
    t1.insert(tk.END,age)
    t1.config(state='disabled')

The Highlighted line is the main statement of the code which calculates the age.

2. Exit Application Button

For the exit application button, we simply create a function that destroys the window and after that add the commad attribute to the button declaration.

The code for the exit function is shown below:

def exit():
    window.destroy()

Complete code for Age Calculator in Python

The whole code is mentioned below:

from datetime import date
today = date.today()

def exit():
    window.destroy()
def get_age():
    d= int(e1.get())
    m=int(e2.get())
    y=int(e3.get())
    age =today.year-y-((today.month, today.day)<(m,d))
    t1.config(state='normal')
    t1.delete('1.0', tk.END)
    t1.insert(tk.END,age)
    t1.config(state='disabled')

import tkinter as tk
window = tk.Tk()
window.geometry("400x300")
window.config(bg="#F7DC6F")
window.resizable(width=False,height=False)
window.title('Age Calculator!')

l1 = tk.Label(window,text="The Age Calculator!",font=("Arial", 20),fg="black",bg="#F7DC6F")
l2 = tk.Label(window,font=("Arial",12),text="Enter your birthday which includes the day-month-year.",fg="black",bg="#F7DC6F")

l_d=tk.Label(window,text="Date: ",font=('Arial',12,"bold"),fg="darkgreen",bg="#F7DC6F")
l_m=tk.Label(window,text="Month: ",font=('Arial',12,"bold"),fg="darkgreen",bg="#F7DC6F")
l_y=tk.Label(window,text="Year: ",font=('Arial',12,"bold"),fg="darkgreen",bg="#F7DC6F")
e1=tk.Entry(window,width=5)
e2=tk.Entry(window,width=5)
e3=tk.Entry(window,width=5)

b1=tk.Button(window,text="Calculate Age!",font=("Arial",13),command=get_age)

l3 = tk.Label(window,text="The Calculated Age is: ",font=('Arial',12,"bold"),fg="darkgreen",bg="#F7DC6F")
t1=tk.Text(window,width=5,height=0,state="disabled")

b2=tk.Button(window,text="Exit Application!",font=("Arial",13),command=exit)

l1.place(x=70,y=5)
l2.place(x=10,y=40)
l_d.place(x=100,y=70)
l_m.place(x=100,y=95)
l_y.place(x=100,y=120)
e1.place(x=180,y=70)
e2.place(x=180,y=95)
e3.place(x=180,y=120)
b1.place(x=100,y=150)
l3.place(x=50,y=200)
t1.place(x=240,y=203)
b2.place(x=100,y=230)

window.mainloop()

Output:

Now that we are all done with the coding part. Let’s run the application! And it works perfectly, the same can be seen in the outputs below.

Output1 Age Calculator
Output1 Age Calculator
Output2 Age Calculator
Output2 Age Calculator

Conclusion

Congratulations! Today you learned how to build your age calculator! Hope you had fun!

Thank you for reading!