GUI Age Calculator

assignment advertisement

Hello friends, today we will make an age calculator using python’s GUI library tkinter so, let’s start.

Python offers a very good GUI library i.e. tkinter which is very easy to learn and use.

Here, we are creating a GUI application to calculate age using a button.

We will ask the user to enter the name and date of birth.

Then we will get that information from the entry widgets and will calculate the user’s age.

At last, we will show the user’s age with the name.

We will use "calculateAge()" function to calculate the age of the user.

So, let’s understand the code now.

Code

# importing the tkinter module
from tkinter import *

# importing date class from datetime module
from datetime import date

# initializing tkinter
root = Tk()

# setting the width and height of gui
root.geometry("700x500")

# setting the title of gui
root.title("Age Calculator")

# loading image 
photo = PhotoImage(file="filename.png")

# creating a Label widget to show the image we load
myimage = Label(image=photo)

# placing the image Label widget using grid method
myimage.grid(row=0, column=1)

# defining function to calculate age according to the data given
# by the user 
def calculateAge():
    # storing today's date in "today" variable
    today = date.today()
    # getting birthdate using .get() method
    birthDate = date(int(yearEntry.get()), int(monthEntry.get()), int(dayEntry.get()))
    # calculating age by subtracting birthdate from today's date
    age = today.year - birthDate.year - ((today.month, today.day) < (birthDate.month, birthDate.day))
    # creating a Label widget to show the calculated age using 
    # grid method
    Label(text=f"{nameValue.get()} your age is {age}").grid(row=6, column=1)


# creating a label widget for asking user his/her name
Label(text="Name").grid(row=1, column=0, padx=90)

# creating a label widget for asking user his/her year of birth
Label(text="Year").grid(row=2, column=0)

# creating a label widget for asking user his/her month of birth
Label(text="Month").grid(row=3, column=0)

# creating a label widget for asking user his/her day of birth
Label(text="Day").grid(row=4, column=0)

# declaring a variable of string datatype to store the name value 
# entered by the user
nameValue = StringVar()

# declaring a variable of string datatype to store the year value
# entered by the user
yearValue = StringVar()

# declaring a variable of string datatype to store the month 
# value entered by the user
monthValue = StringVar()

# declaring a variable of string datatype to store the day value
# entered by the user
dayValue = StringVar()

# creating an entry widget to take name value
nameEntry = Entry(root, textvariable=nameValue)

# creating an entry widget to take year value
yearEntry = Entry(root, textvariable=yearValue)

# creating an entry widget to take month value
monthEntry = Entry(root, textvariable=monthValue)

# creating an entry widget to take day value
dayEntry = Entry(root, textvariable=dayValue)

# placing the entry widgets
nameEntry.grid(row=1, column=1, pady=10)
yearEntry.grid(row=2, column=1, pady=10)
monthEntry.grid(row=3, column=1, pady=10)
dayEntry.grid(row=4, column=1, pady=10)

# creating and placing a button to calculate a show age on 
# clicking on this button
Button(text="Calculate age", command=calculateAge).grid(row=5, column=1, pady=10)

# mainloop() is an infinite loop used to run the application when 
# it's in ready state
root.mainloop()

Thanks for visiting

Keep up with us for more amazing content like this GUI Age Calculator


Watch YouTube Video:



Also read:

Share:
Avatar of Harry

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at [email protected] Thank you

4 thoughts on “GUI Age Calculator

  1. Hi sir/mam how can we create a forms & all with the help of python language. & Which libraries I have to download or install in anaconda jupyter notebook. Rather in any Python compiler like pycharm, python 3.8 and so on.

    1. To create a form as a gui application you need to install
      gui libraries like tkinter where you can create forms using
      entry widget in tkinter we suggest you this tutorial for
      better understanding of entry widget, this is our tutorial
      to change gui screen size from the screen itself

  2. How to insert images, screenshot & all types of pics & files with the help of Python language

    1. Please specify correctly where you want to insert images or files for example if you want to insert it on a gui screen then we recommend you this tutorial

Comments are closed.