-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathage_calculator.py
46 lines (36 loc) · 1.29 KB
/
age_calculator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import tkinter as tk
from tkinter import messagebox
from datetime import datetime
# Function to calculate age
def calculate_age():
try:
birth_date = datetime.strptime(dob_entry.get(), "%Y-%m-%d")
today = datetime.today()
age = today.year - birth_date.year - ((today.month, today.day) < (birth_date.month, birth_date.day))
result_label.config(text=f"You are {age} years old.")
except ValueError:
messagebox.showerror("Invalid Date", "Please enter a valid date in YYYY-MM-DD format.")
# Function to clear the input
def clear_input():
dob_entry.delete(0, tk.END)
result_label.config(text="")
# Setting up the GUI
root = tk.Tk()
root.title("Age Calculator")
root.geometry("300x200")
# Label for Date of Birth
tk.Label(root, text="Enter your Date of Birth (YYYY-MM-DD):").pack(pady=10)
# Entry widget for Date of Birth
dob_entry = tk.Entry(root)
dob_entry.pack(pady=5)
# Button to Calculate Age
calculate_button = tk.Button(root, text="Calculate Age", command=calculate_age)
calculate_button.pack(pady=10)
# Button to Clear the Input
clear_button = tk.Button(root, text="Clear", command=clear_input)
clear_button.pack(pady=5)
# Label to display the result
result_label = tk.Label(root, text="")
result_label.pack(pady=10)
# Run the GUI event loop
root.mainloop()