1
1
# import libraries
2
+
2
3
import tkinter as tk
3
4
from datetime import date
4
5
@@ -26,7 +27,11 @@ def run(self):
26
27
# label for year in which user was born
27
28
self .l2 = tk .Label (text = "Year: " , font = "courier 10" , bg = "lightblue" )
28
29
self .l2 .grid (row = 2 , column = 0 )
30
+ #yearValue = tk.StringVar()
31
+ #adding some checking to ensure its valid
29
32
yearValue = tk .StringVar ()
33
+ #while yearValue.get() > 12 or yearValue.get() < 0:
34
+ # yearValye = tk.IntVar()
30
35
self .yearEntry = tk .Entry (self .master , textvariable = yearValue , relief = "solid" )
31
36
self .yearEntry .grid (row = 2 , column = 1 , padx = 10 , pady = 10 )
32
37
@@ -44,17 +49,66 @@ def run(self):
44
49
self .dayEntry = tk .Entry (self .master , textvariable = dayValue , relief = "solid" )
45
50
self .dayEntry .grid (row = 4 , column = 1 , padx = 10 , pady = 10 )
46
51
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
+
47
99
# defining the function for calculating age
48
100
def ageCalc ():
49
101
self .statement .destroy ()
50
102
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 )
58
112
59
113
# create a button for calculating age
60
114
self .button = tk .Button (text = "Calculate age" , font = "courier 12 bold" , fg = "white" , bg = "dodgerblue" , command = ageCalc )
@@ -67,4 +121,4 @@ def ageCalc():
67
121
if __name__ == '__main__' :
68
122
age_calc = App ()
69
123
age_calc .run ()
70
-
124
+
0 commit comments