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

Python Calculator Program with GUI Intercface

   Python Calculator Program with GUI Intercface Here is a Python program using the tkinter library to create a simple GUI-based calculator. This calculator can perform basic arithmetic operations like addition, subtraction, multiplication, and division.   import tkinter as tk # Function to update the input field when a button is clicked def button_click(item):     global expression     expression = expression + str(item)     input_text.set(expression) # Function to clear the input field def button_clear():     global expression     expression = ""     input_text.set("") # Function to evaluate the expression and display the result def button_equal():     global expression     try:         result = str(eval(expression))  # Evaluate the expression         input_text.set(result) ...

Python Basic Programs to Practice

  Python Programming Introduction Python is a high-level, interpreted programming language known for its simplicity, readability, and ease of learning. It's versatile, suitable for various applications, including web development, data science, automation, artificial intelligence, and more. Python's popularity stems from its vast standard library, extensive community support, and compatibility with other programming languages. Key Features of Python Simple and Easy to Learn : Python's syntax is straightforward, making it a great choice for beginners. Interpreted Language : Code is executed line-by-line, making debugging easier. Object-Oriented : Supports classes and objects, promoting organized, reusable code. Extensive Libraries : Python has a large standard library for handling various tasks and third-party libraries for specialized needs. Cross-Platform : Python can run on various operating systems, including Windows, macOS, and Linux. Python Basic Syntax Variables :...