B) Write a Python GUI program to accept your birthdate and output your age when a button is pressed.

Aim

To write a Python GUI program that accepts the user’s birthdate and displays the calculated age when a button is pressed.


Algorithm

  1. Start the program.
  2. Import the required modules (tkinter and datetime).
  3. Create a GUI window using Tk().
  4. Add labels and entry fields to accept day, month, and year of birth.
  5. Create a function to calculate age using the current date.
  6. Add a button that calls the function when clicked.
  7. Display the calculated age on the screen.
  8. Run the GUI window.
  9. 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)

  1. What is GUI in Python?
  2. Which library is used to create GUI in Python?
  3. What is Tkinter?
  4. What is the use of the Entry widget?
  5. What is the purpose of the Button widget?
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top