Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 063e2a5

Browse files
added some conditional checking, and some messages output to the GUI
1 parent 38f5797 commit 063e2a5

File tree

1 file changed

+62
-8
lines changed

1 file changed

+62
-8
lines changed

‎Age-Calculator-GUI/age_calc_gui.py

Lines changed: 62 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

@@ -26,7 +27,11 @@ def run(self):
2627
# label for year in which user was born
2728
self.l2 = tk.Label(text="Year: ", font="courier 10", bg="lightblue")
2829
self.l2.grid(row=2, column=0)
30+
#yearValue = tk.StringVar()
31+
#adding some checking to ensure its valid
2932
yearValue = tk.StringVar()
33+
#while yearValue.get() > 12 or yearValue.get() < 0:
34+
# yearValye = tk.IntVar()
3035
self.yearEntry = tk.Entry(self.master, textvariable=yearValue, relief="solid")
3136
self.yearEntry.grid(row=2, column=1, padx=10, pady=10)
3237

@@ -44,17 +49,66 @@ def run(self):
4449
self.dayEntry = tk.Entry(self.master, textvariable=dayValue, relief="solid")
4550
self.dayEntry.grid(row=4, column=1, padx=10, pady=10)
4651

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

59113
# create a button for calculating age
60114
self.button = tk.Button(text="Calculate age", font="courier 12 bold", fg="white", bg="dodgerblue", command=ageCalc)
@@ -67,4 +121,4 @@ def ageCalc():
67121
if __name__ == '__main__':
68122
age_calc = App()
69123
age_calc.run()
70-
124+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /