Age Calculator using Python Tkinter

An age calculator in Python using Tkinter is a GUI (Graphical User Interface) application that allows users to determine their age based on their date of birth (DOB). To design the user interface of this application, we use various methods provided by the Tkinter library such as Tk(), pack(), Entry(), Button(), and more.

GUI of Age Calculator

Let us build a GUI-based age calculator where the user can enter their date of birth in the format of YYYY-MM-DD. The application will calculate and display the age in years, months, and days.

Age Calculator Enter your date of birth (YYYY-MM-DD): 1990-05-15 Calculate Age You are 33 years, 8 months, and 12 days old.

Creating an Age Calculator Application using Python Tkinter

To create an age calculator using Python Tkinter, we use the widgets and labels provided using built-in methods. We also implement exception handling so that if there is any wrong input, the program does not crash.

Here are the steps to create a GUI interface along with the logical functionalities for an age calculator:

Step 1: Import the required libraries like tkinter for GUI building and datetime for date operations.

Step 2: Create the tkinter application and set the title.

Step 3: Add the GUI elements like labels, widget for DOB input, and calculation button.

Step 4: Define the function for age calculation where we perform its logical functionalities such as:

  • Retrieve the user data inputs.
  • Convert it to a date object.
  • Calculate the age in terms of years, months, and days.
  • Adjust for incomplete months and days.
  • Display the result or show an error if the input format is incorrect.

Step 5: Run the application with the help of mainloop().

Python Tkinter Code for Age Calculator

Here is the complete code to create a GUI application of age calculator based on above steps:

import tkinter as tk
from datetime import date

def calculate_age():
    dob = entry_date.get()
    try:
        dob_date = date.fromisoformat(dob)
        today = date.today()

        # Calculate year differences
        years = today.year - dob_date.year
        months = today.month - dob_date.month
        days = today.day - dob_date.day

        # Adjust if birth month/day has not occurred yet this year
        if days < 0:
            months -= 1
            # Get days in previous month
            if today.month == 1:
                prev_month = 12
                prev_year = today.year - 1
            else:
                prev_month = today.month - 1
                prev_year = today.year
            
            days_in_prev_month = (date(prev_year, prev_month + 1, 1) - date(prev_year, prev_month, 1)).days
            days += days_in_prev_month
            
        if months < 0:
            years -= 1
            months += 12

        label_result.config(
            text=f"You are {years} years, {months} months, and {days} days old."
        )
    except ValueError:
        label_result.config(text="Please enter a valid date in YYYY-MM-DD format.")

# GUI setup
app = tk.Tk()
app.title("Age Calculator")
app.geometry("400x200")

label_date = tk.Label(app, text="Enter your date of birth (YYYY-MM-DD):")
label_date.pack(pady=10)

entry_date = tk.Entry(app, width=20)
entry_date.pack(pady=5)

button_calculate = tk.Button(app, text="Calculate Age", command=calculate_age)
button_calculate.pack(pady=10)

label_result = tk.Label(app, text="", fg="green", font=("Arial", 10))
label_result.pack(pady=10)

app.mainloop()

Example Usage

When you run the above program, it creates a window with:

  • A label prompting for date of birth input
  • An entry field to input the date in YYYY-MM-DD format
  • A "Calculate Age" button to trigger the calculation
  • A result label to display the calculated age

For example, if you enter "1990-05-15" and click "Calculate Age", the application will display your current age in years, months, and days.

Key Features

  • Date Validation: Uses try-except block to handle invalid date formats
  • Accurate Calculation: Properly adjusts for months and days that haven't occurred yet
  • User-Friendly Interface: Simple and clean GUI with clear labels
  • Error Handling: Displays helpful error messages for invalid inputs

Conclusion

Building a GUI-based age calculator in Python using Tkinter demonstrates how to create interactive desktop applications. The calculator accurately computes age in years, months, and days while providing proper error handling for invalid date inputs.

Updated on: 2026-03-27T12:23:44+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements