Skip to content

Commit de6dcd1

Browse files
Merge pull request avinashkranjan#1346 from justwhyit/master
Age_Calc_Gui input validation and error handling
2 parents 38f5797 + 2f5d5e6 commit de6dcd1

File tree

1 file changed

+61
-8
lines changed

1 file changed

+61
-8
lines changed

Age-Calculator-GUI/age_calc_gui.py

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# import libraries
2+
23
import tkinter as tk
34
from datetime import date
45

@@ -44,17 +45,69 @@ def run(self):
4445
self.dayEntry = tk.Entry(self.master, textvariable=dayValue, relief="solid")
4546
self.dayEntry.grid(row=4, column=1, padx=10, pady=10)
4647

48+
49+
def check_year():
50+
#simple method to check the validity of a user input birth year
51+
self.statement.destroy()
52+
today = date.today()
53+
try:
54+
year = int(self.yearEntry.get())
55+
if today.year - year < 0:
56+
self.statement = tk.Label(text=f"{nameValue.get()}'s age cannot be negative.", font="courier 10", bg="lightblue")
57+
self.statement.grid(row=6, column=1, pady=15)
58+
return False
59+
else:
60+
return True
61+
except Exception as e:
62+
self.statement = tk.Label(text=f"{nameValue.get()}'s birth year cannot parse to int.", font="courier 10", bg="lightblue")
63+
self.statement.grid(row=6, column=1, pady=15)
64+
return False
65+
66+
def check_month():
67+
#simple method to check the validity of a user input birth month
68+
self.statement.destroy()
69+
try:
70+
month = int(self.monthEntry.get())
71+
if month < 0 or month > 12:
72+
self.statement = tk.Label(text=f"{nameValue.get()}'s birth month is outside 1-12.", font="courier 10", bg="lightblue")
73+
self.statement.grid(row=6, column=1, pady=15)
74+
return False
75+
else:
76+
return True
77+
except Exception as e:
78+
self.statement = tk.Label(text=f"{nameValue.get()}'s birth month cannot parse to int.", font="courier 10", bg="lightblue")
79+
self.statement.grid(row=6, column=1, pady=15)
80+
return False
81+
82+
def check_day():
83+
#simple method to check the validity of a user input birth day
84+
self.statement.destroy()
85+
try:
86+
day = int(self.dayEntry.get())
87+
if day < 0 or day > 31:
88+
self.statement = tk.Label(text=f"{nameValue.get()}'s birth day is outside 1-31.", font="courier 10", bg="lightblue")
89+
self.statement.grid(row=6, column=1, pady=15)
90+
return False
91+
else:
92+
return True
93+
except Exception as e:
94+
self.statement = tk.Label(text=f"{nameValue.get()}'s birth month cannot parse to int.", font="courier 10", bg="lightblue")
95+
self.statement.grid(row=6, column=1, pady=15)
96+
return False
97+
4798
# defining the function for calculating age
4899
def ageCalc():
49100
self.statement.destroy()
50101
today = date.today()
51-
birthDate = date(int(self.yearEntry.get()), int(
52-
self.monthEntry.get()), int(self.dayEntry.get()))
53-
age = today.year - birthDate.year
54-
if today.month < birthDate.month or today.month == birthDate.month and today.day < birthDate.day:
55-
age -= 1
56-
self.statement = tk.Label(text=f"{nameValue.get()}'s age is {age}.", font="courier 10", bg="lightblue")
57-
self.statement.grid(row=6, column=1, pady=15)
102+
#adding some stuff for checking validity of inputs
103+
if check_year() and check_month() and check_day():
104+
birthDate = date(int(self.yearEntry.get()), int(
105+
self.monthEntry.get()), int(self.dayEntry.get()))
106+
age = today.year - birthDate.year
107+
if today.month < birthDate.month or today.month == birthDate.month and today.day < birthDate.day:
108+
age -= 1
109+
self.statement = tk.Label(text=f"{nameValue.get()}'s age is {age}.", font="courier 10", bg="lightblue")
110+
self.statement.grid(row=6, column=1, pady=15)
58111

59112
# create a button for calculating age
60113
self.button = tk.Button(text="Calculate age", font="courier 12 bold", fg="white", bg="dodgerblue", command=ageCalc)
@@ -67,4 +120,4 @@ def ageCalc():
67120
if __name__ == '__main__':
68121
age_calc = App()
69122
age_calc.run()
70-
123+

0 commit comments

Comments
 (0)