GUI Age Calculator using python

            Welcome to GUI Age Calculator



This is GUI Age Calculator using python you can more modify to this calculator.






import tkinter as tk
from tkinter import ttk
from datetime import date


class App:
def __init__(self):
self.master = tk.Tk()
self.master.configure(bg="lightblue")
self.master.title('Age Calculator')
self.master.attributes('-fullscreen', True)
self.statement = tk.Label(self.master)

# Add close button
self.close_button = tk.Button(self.master, text="X", bg="red", fg="white", font="Helvetica 10 bold", command=self.close)
self.close_button.place(relx=0.30, rely=0.050, anchor="ne")

# Function to close the window
def close(self):
self.master.destroy()

def run(self):
# Label and Entry for Name
self.l1 = tk.Label(text="Name: ", font="courier 10", bg="lightblue")
self.l1.grid(row=1, column=0)
nameValue = tk.StringVar()
self.nameEntry = tk.Entry(
self.master, textvariable=nameValue, relief="solid")
self.nameEntry.grid(row=1, column=1, padx=10, pady=10)

# Label and Entry for Year
self.l2 = tk.Label(text="Year: ", font="courier 10", bg="lightblue")
self.l2.grid(row=2, column=0)
yearValue = tk.StringVar()
self.yearEntry = tk.Entry(
self.master, textvariable=yearValue, relief="solid")
self.yearEntry.grid(row=2, column=1, padx=10, pady=10)

# Label and Combobox for Month
self.l3 = tk.Label(text="Month: ", font="courier 10", bg="lightblue")
self.l3.grid(row=3, column=0)
monthValue = tk.StringVar()
self.monthCombobox = ttk.Combobox(self.master, textvariable=monthValue, state="readonly", width=10)
self.monthCombobox['values'] = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December']
self.monthCombobox.grid(row=3, column=1, padx=10, pady=10)

# Label and Combobox for Day
self.l4 = tk.Label(text="Day: ", font="courier 10", bg="lightblue")
self.l4.grid(row=4, column=0)
dayValue = tk.StringVar()
self.dayCombobox = ttk.Combobox(self.master, textvariable=dayValue, state="readonly", width=5)
self.dayCombobox.grid(row=4, column=1, padx=10, pady=10)

# Function to update days based on selected month
def update_days(*args):
days = 31
month = monthValue.get()
if month in ['April', 'June', 'September', 'November']: # Months with 30 days
days = 30
elif month == 'February': # February
year = yearValue.get()
if (int(year) % 4 == 0 and int(year) % 100 != 0) or int(year) % 400 == 0:
days = 29 # Leap year
else:
days = 28
self.dayCombobox['values'] = [str(day) for day in range(1, days + 1)]

monthValue.trace('w', update_days)

# Function to validate name input
def check_name():
self.statement.destroy()
try:
for i in nameValue.get():
if not (i.isalpha() or i.isspace()):
self.statement = tk.Label(
text=f"{nameValue.get()} is not a valid name! Please enter a valid name.", font="courier 10", bg="lightblue")
self.statement.grid(row=6, column=1, pady=15)
return False
return True
except Exception as e:
self.statement = tk.Label(
text="Please try with a different name.", font="courier 10", bg="lightblue")
self.statement.grid(row=6, column=1, pady=15)
return False

# Function to validate birth year input
def check_year():
self.statement.destroy()
today = date.today()
try:
year = int(self.yearEntry.get())
if today.year - year < 0:
self.statement = tk.Label(
text=f"{nameValue.get()}'s age cannot be negative.", font="courier 10", bg="lightblue")
self.statement.grid(row=6, column=1, pady=15)
return False
else:
return True
except ValueError:
self.statement = tk.Label(
text=f"{nameValue.get()}'s birth year cannot be parsed to an integer.", font="courier 10", bg="lightblue")
self.statement.grid(row=6, column=1, pady=15)
return False

# Function to validate birth month input
def check_month():
self.statement.destroy()
try:
month = monthValue.get()
if month not in ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December']:
self.statement = tk.Label(
text=f"{nameValue.get()}'s birth month is not valid.", font="courier 10", bg="lightblue")
self.statement.grid(row=6, column=1, pady=15)
return False
else:
return True
except Exception as e:
self.statement = tk.Label(
text=f"{nameValue.get()}'s birth month cannot be parsed.", font="courier 10", bg="lightblue")
self.statement.grid(row=6, column=1, pady=15)
return False

# Function to validate birth day input
def check_day():
self.statement.destroy()
try:
day = int(self.dayCombobox.get())
if day < 1 or day > 31:
self.statement = tk.Label(
text=f"{nameValue.get()}'s birth day is outside 1-31.", font="courier 10", bg="lightblue")
self.statement.grid(row=6, column=1, pady=15)
return False
else:
return True
except ValueError:
self.statement = tk.Label(
text=f"{nameValue.get()}'s birth day cannot be parsed to an integer.", font="courier 10", bg="lightblue")
self.statement.grid(row=6, column=1, pady=15)
return False

# Function to calculate age
def ageCalc():
self.statement.destroy()
today = date.today()
if check_name() and check_year() and check_month() and check_day():
month_dict = {'January': 1, 'February': 2, 'March': 3, 'April': 4, 'May': 5, 'June': 6, 'July': 7,
'August': 8, 'September': 9, 'October': 10, 'November': 11, 'December': 12}
birthDate = date(int(self.yearEntry.get()), month_dict[self.monthCombobox.get()],
int(self.dayCombobox.get()))
age = today.year - birthDate.year
if today.month < month_dict[self.monthCombobox.get()] or (
today.month == month_dict[self.monthCombobox.get()] and today.day < birthDate.day):
age -= 1

# Calculate total months
total_months = (today.year - birthDate.year) * 12 + today.month - month_dict[self.monthCombobox.get()]

# Calculate total hours and minutes
total_hours = age * 24
total_minutes = total_hours * 60

self.statement = tk.Label(
text=f"{nameValue.get()}'s age is {age} years, {total_months} months, {total_hours} hours, and {total_minutes} minutes.",
font="courier 10", bg="lightblue")
self.statement.grid(row=6, column=1, pady=15)

# Button to trigger age calculation
self.button = tk.Button(text="Calculate age", font="courier 12 bold",
fg="white", bg="dodgerblue", command=ageCalc)
self.button.grid(row=5, column=1)

self.master.mainloop()


if __name__ == '__main__':
age_calc = App()
age_calc.run()

Comments

Popular posts from this blog

Screen Recording using python.

wallpaper changer.

Find any color from image using python