Skip to content

Commit 2c39331

Browse files
Merge pull request #1789 from Shiv-Expert2503/v2
'Fixed_name_section'
2 parents a4eb1c3 + bb570eb commit 2c39331

File tree

1 file changed

+59
-21
lines changed

1 file changed

+59
-21
lines changed

Age-Calculator-GUI/age_calc_gui.py

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from datetime import date
55

66
# GUI App class
7+
8+
79
class App:
810
def __init__(self):
911
# initialized window
@@ -21,103 +23,139 @@ def run(self):
2123
nameValue = tk.StringVar()
2224

2325
# creating a entry box for input
24-
self.nameEntry = tk.Entry(self.master, textvariable=nameValue, relief="solid")
26+
self.nameEntry = tk.Entry(
27+
self.master, textvariable=nameValue, relief="solid")
2528
self.nameEntry.grid(row=1, column=1, padx=10, pady=10)
2629

2730
# label for year in which user was born
2831
self.l2 = tk.Label(text="Year: ", font="courier 10", bg="lightblue")
2932
self.l2.grid(row=2, column=0)
3033
yearValue = tk.StringVar()
31-
self.yearEntry = tk.Entry(self.master, textvariable=yearValue, relief="solid")
34+
self.yearEntry = tk.Entry(
35+
self.master, textvariable=yearValue, relief="solid")
3236
self.yearEntry.grid(row=2, column=1, padx=10, pady=10)
3337

3438
# label for month in which user was born
3539
self.l3 = tk.Label(text="Month: ", font="courier 10", bg="lightblue")
3640
self.l3.grid(row=3, column=0)
3741
monthValue = tk.StringVar()
38-
self.monthEntry = tk.Entry(self.master, textvariable=monthValue, relief="solid")
42+
self.monthEntry = tk.Entry(
43+
self.master, textvariable=monthValue, relief="solid")
3944
self.monthEntry.grid(row=3, column=1, padx=10, pady=10)
4045

4146
# label for day/date on which user was born
4247
self.l4 = tk.Label(text="Day: ", font="courier 10", bg="lightblue")
4348
self.l4.grid(row=4, column=0)
4449
dayValue = tk.StringVar()
45-
self.dayEntry = tk.Entry(self.master, textvariable=dayValue, relief="solid")
50+
self.dayEntry = tk.Entry(
51+
self.master, textvariable=dayValue, relief="solid")
4652
self.dayEntry.grid(row=4, column=1, padx=10, pady=10)
4753

54+
def check_name():
55+
# simple method to check the validity of a user input name
56+
self.statement.destroy()
57+
try:
58+
# checking if name has only lower and upper case alphabets is yes then valid else not valid
59+
for i in nameValue.get():
60+
if (i >= 'a' and i <= 'z') or (i >= 'A' and i <= 'Z'):
61+
pass
62+
else:
63+
self.statement = tk.Label(
64+
text=f"{nameValue.get()} is not a\n valid name! Please\n enter a valid name .", font="courier 10", bg="lightblue")
65+
self.statement.grid(row=6, column=1, pady=15)
66+
return False
67+
68+
return True
69+
# the code will never go to this except statement but we have written this just to be at a safer side
70+
except Exception as e:
71+
self.statement = tk.Label(
72+
text="Please try with a\n different name.", font="courier 10", bg="lightblue")
73+
self.statement.grid(row=6, column=1, pady=15)
74+
return False
4875

4976
def check_year():
50-
#simple method to check the validity of a user input birth year
77+
# simple method to check the validity of a user input birth year
5178
self.statement.destroy()
5279
today = date.today()
5380
try:
5481
year = int(self.yearEntry.get())
5582
if today.year - year < 0:
56-
self.statement = tk.Label(text=f"{nameValue.get()}'s age\n cannot be negative.", font="courier 10", bg="lightblue")
83+
self.statement = tk.Label(
84+
text=f"{nameValue.get()}'s age\n cannot be negative.", font="courier 10", bg="lightblue")
5785
self.statement.grid(row=6, column=1, pady=15)
5886
return False
5987
else:
6088
return True
6189
except Exception as e:
62-
self.statement = tk.Label(text=f"{nameValue.get()}'s birth year\n cannot parse to int.", font="courier 10", bg="lightblue")
90+
self.statement = tk.Label(
91+
text=f"{nameValue.get()}'s birth year\n cannot parse to int.", font="courier 10", bg="lightblue")
6392
self.statement.grid(row=6, column=1, pady=15)
6493
return False
6594

6695
def check_month():
67-
#simple method to check the validity of a user input birth month
96+
# simple method to check the validity of a user input birth month
6897
self.statement.destroy()
6998
try:
7099
month = int(self.monthEntry.get())
71100
if month < 0 or month > 12:
72-
self.statement = tk.Label(text=f"{nameValue.get()}'s birth month\n is outside 1-12.", font="courier 10", bg="lightblue")
101+
self.statement = tk.Label(
102+
text=f"{nameValue.get()}'s birth month\n is outside 1-12.", font="courier 10", bg="lightblue")
73103
self.statement.grid(row=6, column=1, pady=15)
74104
return False
75105
else:
76106
return True
77107
except Exception as e:
78-
self.statement = tk.Label(text=f"{nameValue.get()}'s birth month\n cannot parse to int.", font="courier 10", bg="lightblue")
108+
self.statement = tk.Label(
109+
text=f"{nameValue.get()}'s birth month\n cannot parse to int.", font="courier 10", bg="lightblue")
79110
self.statement.grid(row=6, column=1, pady=15)
80111
return False
81-
112+
82113
def check_day():
83-
#simple method to check the validity of a user input birth day
114+
# simple method to check the validity of a user input birth day
84115
self.statement.destroy()
85116
try:
86117
day = int(self.dayEntry.get())
87118
if day < 0 or day > 31:
88-
self.statement = tk.Label(text=f"{nameValue.get()}'s birth day is\n outside 1-31.", font="courier 10", bg="lightblue")
119+
self.statement = tk.Label(
120+
text=f"{nameValue.get()}'s birth day is\n outside 1-31.", font="courier 10", bg="lightblue")
89121
self.statement.grid(row=6, column=1, pady=15)
90122
return False
91123
else:
92124
return True
93125
except Exception as e:
94-
self.statement = tk.Label(text=f"{nameValue.get()}'s birth day\n cannot parse to int.", font="courier 10", bg="lightblue")
126+
127+
self.statement = tk.Label(
128+
text=f"{nameValue.get()}'s birth month\n cannot parse to int.", font="courier 10", bg="lightblue")
129+
130+
131+
95132
self.statement.grid(row=6, column=1, pady=15)
96133
return False
97134

98135
# defining the function for calculating age
99136
def ageCalc():
100137
self.statement.destroy()
101138
today = date.today()
102-
#adding some stuff for checking validity of inputs
103-
if check_year() and check_month() and check_day():
139+
# adding some stuff for checking validity of inputs
140+
if check_name() and check_year() and check_month() and check_day():
104141
birthDate = date(int(self.yearEntry.get()), int(
105142
self.monthEntry.get()), int(self.dayEntry.get()))
106143
age = today.year - birthDate.year
107144
if today.month < birthDate.month or today.month == birthDate.month and today.day < birthDate.day:
108145
age -= 1
109-
self.statement = tk.Label(text=f"{nameValue.get()}'s age is {age}.", font="courier 10", bg="lightblue")
146+
self.statement = tk.Label(
147+
text=f"{nameValue.get()}'s age is {age}.", font="courier 10", bg="lightblue")
110148
self.statement.grid(row=6, column=1, pady=15)
111149

112150
# create a button for calculating age
113-
self.button = tk.Button(text="Calculate age", font="courier 12 bold", fg="white", bg="dodgerblue", command=ageCalc)
151+
self.button = tk.Button(text="Calculate age", font="courier 12 bold",
152+
fg="white", bg="dodgerblue", command=ageCalc)
114153
self.button.grid(row=5, column=1)
115-
154+
116155
# infinite loop to run program
117156
self.master.mainloop()
118-
157+
119158

120159
if __name__ == '__main__':
121160
age_calc = App()
122161
age_calc.run()
123-

0 commit comments

Comments
 (0)