How To Create A GUI Age Calculator in Python Tkinter

Filed Under: Python
AgeCalculator FeaImg

You’ve come to the correct spot if you want to develop a visually appealing app with Tkinter. We’ll make an age calculator app with Python’s Tkinter framework in this post.

Users may enter their date of birth into this age calculator software, and the program will compute and show their age. Isn’t it awesome? Let’s put this concept into a fun Python program.

Implementing A GUI Age Calculator in Python Tkinter

To begin the project, launch your code editor. First and foremost, we must import three libraries into our code.

The Tkinter library is the first. To work with dates, we’ll need the datetime library. Finally, we require the PIL library, which will assist us with working with pictures.

import datetime
import tkinter as tk
from PIL import Image,ImageTk

Now, let’s make a basic window for our program and call it “Age Cal App.”

window=tk.Tk()
window.title("Age_Cal_App")

Then we’ll make four labels, one for each of the name, year, month, and date, and place them on the grid.

name = tk.Label(text = "Name")
name.grid(column=0,row=1)
year = tk.Label(text = "Year")
year.grid(column=0,row=2)
month = tk.Label(text = "Month")
month.grid(column=0,row=3)
date = tk.Label(text = "Day")
date.grid(column=0,row=4)

We’ll make entry fields to collect user inputs for all of the labels we’ve made. Place them on the right side of the respective labels using the grid approach.

nameEntry = tk.Entry()
nameEntry.grid(column=1,row=1)
yearEntry = tk.Entry()
yearEntry.grid(column=1,row=2)
monthEntry = tk.Entry()
monthEntry.grid(column=1,row=3)
dateEntry = tk.Entry()
dateEntry.grid(column=1,row=4)

Then we’ll construct a function to collect user input. That function is known as getInput().

Within that function, we will construct an instance of the Person class (which will be defined later) and feed the name, and birth date to the Person class’s “init” method.

To convert values into integer representation, we utilize the standard int() function. Then, as output, we construct a text field that displays the user’s age.

def getInput():
    name = nameEntry.get()
    monkey = Person(name,datetime.date(int(yearEntry.get()),int(monthEntry.get()),int(dateEntry.get())))
    
    textArea = tk.Text(master=window,height=10,width=25)
    textArea.grid (column=1,row=6)
    answer = "Heyy {monkey}!!!. You are {age} years old!!! ".format(monkey=name, age=monkey.age())
    textArea.insert(tk.END,answer)

I’m aware that my variable names are unusual. A monkey appeared out of nowhere as I was naming a variable. You can, however, use a new name that is better than my strange ones.

Then, we’ll make a button for people to submit their input values. The button is linked to the getInput method.

button=tk.Button(window,text="Calculate Age",command=getInput,bg="pink")
button.grid(column=1,row=5)

Let us now define the Person class. We’ll also write the init method and the aging method, which will compute the user’s age by subtracting the user’s birth date from today’s date.

class Person:
    def __init__(self,name,birthdate):
        self.name = name
        self.birthdate = birthdate
    def age(self):
        today = datetime.date.today()
        age = today.year-self.birthdate.year
        return age

Next, we’ll add a picture to our app to make it visually appealing. You must ensure that the picture is in the same folder as the Python program. Don’t forget to alter the image’s name as well.

from PIL import Image,ImageTk  #import the PIL library to our code
image = Image.open('app_image.png' )     #specify the path of the image
image.thumbnail((200,200),Image.ANTIALIAS)    #100,100 is the resolution
#ANTIALIAS helps to deal some problems while using images
photo= ImageTk.PhotoImage(image)   #converts the image to a tkinter image
label_image= tk.Label(image=photo)  #stores the image in a label
label_image.grid(column=1,row=0)    #Puts it in a grid

Finally, using the mainloop() function, let’s execute everything inside the window.

window.mainloop()

Complete Code of Age Calculator

That’s all. Here’s a brief rundown of the code we wrote to construct the app.

That’s all. Here’s a brief rundown of the full code.

import datetime
import tkinter as tk
from PIL import Image,ImageTk

window=tk.Tk()
window.title("Age_Cal_App")

name = tk.Label(text = "Name")
name.grid(column=0,row=1)
year = tk.Label(text = "Year")
year.grid(column=0,row=2)
month = tk.Label(text = "Month")
month.grid(column=0,row=3)
date = tk.Label(text = "Day")
date.grid(column=0,row=4)

nameEntry = tk.Entry()
nameEntry.grid(column=1,row=1)
yearEntry = tk.Entry()
yearEntry.grid(column=1,row=2)
monthEntry = tk.Entry()
monthEntry.grid(column=1,row=3)
dateEntry = tk.Entry()
dateEntry.grid(column=1,row=4)

def getInput():
    name=nameEntry.get()
    monkey = Person(name, datetime.date(int(yearEntry.get()), int(monthEntry.get()), int(dateEntry.get())))
    
    textArea = tk.Text(master=window,height=10,width=25)
    textArea.grid(column=1,row=6)
    answer = " Heyy {monkey}!!!. You are {age} years old!!! ".format(monkey=name, age=monkey.age())
    textArea.insert(tk.END,answer)

button=tk.Button(window,text="Calculate Age",command=getInput,bg="pink")
button.grid(column=1,row=5)

class Person:
    def __init__(self,name,birthdate):
        self.name = name
        self.birthdate = birthdate
    def age(self):
        today = datetime.date.today()
        age = today.year-self.birthdate.year
        return age
    
from PIL import Image,ImageTk  #import the PIL library to our code
image = Image.open('app_image.png' )     #specify the path of the image
image.thumbnail((200,200),Image.ANTIALIAS)    #100,100 is the resolution
#ANTIALIAS helps to deal some problems while using images
photo= ImageTk.PhotoImage(image)   #converts the image to a tkinter image
label_image= tk.Label(image=photo)  #stores the image in a label
label_image.grid(column=1,row=0)    #Puts it in a grid

window.mainloop()

The app’s user interface will appear like this when we run the code.

Age Calculator Output Screen
Age Calculator Output Screen

Sample Output

Age Calculator Sample Output
Age Calculator Sample Output

Conclusion

We used Python to construct a fantastic-looking program, and I hope you did an incredible job. Congratulations on the completion of your age calculator app!

If you have any questions or comments, please leave them in the comments area. I will gladly assist you.

It’s now time to hone your programming talents. Try to develop as many applications and projects as you can.

close
Generic selectors
Exact matches only
Search in title
Search in content