Aim
To write a Python GUI program that accepts the user’s birthdate and displays the calculated age when a button is pressed.
Algorithm
- Start the program.
- Import the required modules (
tkinteranddatetime). - Create a GUI window using
Tk(). - Add labels and entry fields to accept day, month, and year of birth.
- Create a function to calculate age using the current date.
- Add a button that calls the function when clicked.
- Display the calculated age on the screen.
- Run the GUI window.
- Stop the program.
Program
# Python GUI program to calculate age from birthdate
from tkinter import *
from datetime import datedef calculate_age():
day = int(day_entry.get())
month = int(month_entry.get())
year = int(year_entry.get()) today = date.today()
age = today.year - year - ((today.month, today.day) < (month, day)) result_label.config(text="Your Age is: " + str(age) + " years")root = Tk()
root.title("Age Calculator")
root.geometry("300x250")Label(root, text="Enter Birth Day").pack()
day_entry = Entry(root)
day_entry.pack()Label(root, text="Enter Birth Month").pack()
month_entry = Entry(root)
month_entry.pack()Label(root, text="Enter Birth Year").pack()
year_entry = Entry(root)
year_entry.pack()Button(root, text="Calculate Age", command=calculate_age).pack(pady=10)result_label = Label(root, text="")
result_label.pack()root.mainloop()
Oral Questions (Viva Questions)
- What is GUI in Python?
- Which library is used to create GUI in Python?
- What is Tkinter?
- What is the use of the Entry widget?
- What is the purpose of the Button widget?
