4
4
from datetime import date
5
5
6
6
# GUI App class
7
+
8
+
7
9
class App :
8
10
def __init__ (self ):
9
11
# initialized window
@@ -21,103 +23,139 @@ def run(self):
21
23
nameValue = tk .StringVar ()
22
24
23
25
# 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" )
25
28
self .nameEntry .grid (row = 1 , column = 1 , padx = 10 , pady = 10 )
26
29
27
30
# label for year in which user was born
28
31
self .l2 = tk .Label (text = "Year: " , font = "courier 10" , bg = "lightblue" )
29
32
self .l2 .grid (row = 2 , column = 0 )
30
33
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" )
32
36
self .yearEntry .grid (row = 2 , column = 1 , padx = 10 , pady = 10 )
33
37
34
38
# label for month in which user was born
35
39
self .l3 = tk .Label (text = "Month: " , font = "courier 10" , bg = "lightblue" )
36
40
self .l3 .grid (row = 3 , column = 0 )
37
41
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" )
39
44
self .monthEntry .grid (row = 3 , column = 1 , padx = 10 , pady = 10 )
40
45
41
46
# label for day/date on which user was born
42
47
self .l4 = tk .Label (text = "Day: " , font = "courier 10" , bg = "lightblue" )
43
48
self .l4 .grid (row = 4 , column = 0 )
44
49
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" )
46
52
self .dayEntry .grid (row = 4 , column = 1 , padx = 10 , pady = 10 )
47
53
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
48
75
49
76
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
51
78
self .statement .destroy ()
52
79
today = date .today ()
53
80
try :
54
81
year = int (self .yearEntry .get ())
55
82
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" )
57
85
self .statement .grid (row = 6 , column = 1 , pady = 15 )
58
86
return False
59
87
else :
60
88
return True
61
89
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" )
63
92
self .statement .grid (row = 6 , column = 1 , pady = 15 )
64
93
return False
65
94
66
95
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
68
97
self .statement .destroy ()
69
98
try :
70
99
month = int (self .monthEntry .get ())
71
100
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" )
73
103
self .statement .grid (row = 6 , column = 1 , pady = 15 )
74
104
return False
75
105
else :
76
106
return True
77
107
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" )
79
110
self .statement .grid (row = 6 , column = 1 , pady = 15 )
80
111
return False
81
-
112
+
82
113
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
84
115
self .statement .destroy ()
85
116
try :
86
117
day = int (self .dayEntry .get ())
87
118
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" )
89
121
self .statement .grid (row = 6 , column = 1 , pady = 15 )
90
122
return False
91
123
else :
92
124
return True
93
125
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
+
95
132
self .statement .grid (row = 6 , column = 1 , pady = 15 )
96
133
return False
97
134
98
135
# defining the function for calculating age
99
136
def ageCalc ():
100
137
self .statement .destroy ()
101
138
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 ():
104
141
birthDate = date (int (self .yearEntry .get ()), int (
105
142
self .monthEntry .get ()), int (self .dayEntry .get ()))
106
143
age = today .year - birthDate .year
107
144
if today .month < birthDate .month or today .month == birthDate .month and today .day < birthDate .day :
108
145
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" )
110
148
self .statement .grid (row = 6 , column = 1 , pady = 15 )
111
149
112
150
# 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 )
114
153
self .button .grid (row = 5 , column = 1 )
115
-
154
+
116
155
# infinite loop to run program
117
156
self .master .mainloop ()
118
-
157
+
119
158
120
159
if __name__ == '__main__' :
121
160
age_calc = App ()
122
161
age_calc .run ()
123
-
0 commit comments