1
1
# import libraries
2
+
2
3
import tkinter as tk
3
4
from datetime import date
4
5
@@ -44,17 +45,69 @@ def run(self):
44
45
self .dayEntry = tk .Entry (self .master , textvariable = dayValue , relief = "solid" )
45
46
self .dayEntry .grid (row = 4 , column = 1 , padx = 10 , pady = 10 )
46
47
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
+
47
98
# defining the function for calculating age
48
99
def ageCalc ():
49
100
self .statement .destroy ()
50
101
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 )
58
111
59
112
# create a button for calculating age
60
113
self .button = tk .Button (text = "Calculate age" , font = "courier 12 bold" , fg = "white" , bg = "dodgerblue" , command = ageCalc )
@@ -67,4 +120,4 @@ def ageCalc():
67
120
if __name__ == '__main__' :
68
121
age_calc = App ()
69
122
age_calc .run ()
70
-
123
+
0 commit comments