Skip to main content

Python GUI Program to Calculate Age

Python GUI Program to Calculate Age

 


 

Certainly! Below is a Python program using the tkinter library to create a simple GUI that accepts a date of birth and calculates the age.

 

import tkinter as tk
from tkinter import messagebox
from datetime import datetime

def calculate_age():
    dob_str = entry_dob.get()
    try:
        dob = datetime.strptime(dob_str, "%Y-%m-%d")
        today = datetime.today()
        age = today.year - dob.year - ((today.month, today.day) < (dob.month, dob.day))
        messagebox.showinfo("Age Calculator", f"Your age is: {age} years")
    except ValueError:
        messagebox.showerror("Error", "Please enter the date in YYYY-MM-DD format")

# Create the main window
root = tk.Tk()
root.title("Age Calculator")

# Create a label and entry for date of birth
label_dob = tk.Label(root, text="Enter your date of birth (YYYY-MM-DD):")
label_dob.pack(pady=10)

entry_dob = tk.Entry(root)
entry_dob.pack(pady=10)

# Create a button to calculate age
button_calculate = tk.Button(root, text="Calculate Age", command=calculate_age)
button_calculate.pack(pady=20)

# Run the application
root.mainloop() 



How the Program Works:

Importing Libraries:

tkinter is used to create the GUI.

datetime is used to handle date operations.

calculate_age Function:

This function retrieves the date of birth from the entry widget.

It then calculates the age by comparing the current date with the date of birth.

If the date is entered in the wrong format, an error message is displayed.

Creating the GUI:

A label and an entry widget are created to accept the date of birth.

A button is provided to trigger the age calculation.

Running the Application:

The mainloop() method is called to start the GUI event loop.

How to Run:

 
Save the code to a file, e.g., age_calculator.py.

Run the file using Python: python age_calculator.py.

Enter your date of birth in the format YYYY-MM-DD and click "Calculate Age".

This will display a message box with your calculated age.

Comments

Popular posts from this blog

Simple Number Guessing Game in Python

  Build a Simple Number Guessing Game in Python: A Beginner-Friendly Project Are you looking to dive into Python programming with a fun and interactive project? The number guessing game Python project is an excellent starting point for beginners. This classic game challenges players to guess a randomly generated number within a limited number of attempts, helping you practice essential concepts like loops, conditionals, and user input. In this blog post, we'll walk through how to create your own Python guessing game project , complete with code and detailed explanations. Whether you're a student, hobbyist, or aspiring developer, this tutorial will boost your skills while keeping things engaging. Why Choose a Number Guessing Game as Your Python Project? The number guessing game Python is one of the most searched beginner projects because it's simple yet versatile. It teaches core programming fundamentals without overwhelming you with complex libraries or frameworks. Her...

Classic Number Guessing Game Using Python

    Build Your First Python Game: The Classic Number Guessing Game! Are you learning Python and looking for a fun, hands-on project to test your skills? You’ve come to the right place. When starting out with programming, it’s easy to get stuck just reading tutorials. The real magic happens when you build something interactive. Today, we’re going to build a classic: The Number Guessing Game . It’s simple concept: The computer picks a random number between 1 and 100, and you have a limited number of tries to guess it. After every guess, the computer tells you if you need to go "Higher" or "Lower." Why This is the Perfect Beginner Project Despite its simplicity, this game is a fantastic learning tool because it combines several fundamental programming concepts into one cohesive package. By building this, you will master: Variables: Storing information like the secret number and attempts left. Loops ( while ): Keeping the game running until the player wins or loses. C...