-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathage_calculator.py
348 lines (262 loc) · 10.2 KB
/
age_calculator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# Import some functions from tkinter module
from tkinter import Entry, Button, Tk, Label, END
# import messagebox class from tkinter
from tkinter import messagebox
# import datetime module
from datetime import datetime, date
# relativedelta of dateutil module gives difference between two dates
from dateutil.relativedelta import relativedelta
# create a object of TK class to create a GUI window
root = Tk()
# set the name of tkinter GUI window
root.title('Age Calculator')
# set the size of root window
root.geometry("700x610")
# set the background color of whole GUI window root
root.config(bg="#344e41")
def today():
# When today button is pressed this button will fill presend date in the field.
present = datetime.now()
day = present.strftime('%d')
month = present.strftime("%m")
year = present.strftime("%Y")
# sending the present day values into given entry field
given_day_entry.insert(END, day)
given_month_entry.insert(END, month)
given_year_entry.insert(END, year)
def clearAll():
# Function for clearing the contents of all text entry boxes
# delete the value of date of birth entry
day_entry.delete(0, END)
month_entry.delete(0, END)
year_entry.delete(0, END)
# delete the value of given entry
given_day_entry.delete(0, END)
given_month_entry.delete(0, END)
given_year_entry.delete(0, END)
# # delete the value of results data
days_value.config(text='')
months_value.config(text='')
years_value.config(text='')
# function for checking error
def checkError():
# if any of the entry field is empty then
# show an error message and clear all the entries
# get the all data inside variables
d = day_entry.get()
m = month_entry.get()
y = year_entry.get()
gd = given_day_entry.get()
gm = given_month_entry.get()
gy = given_year_entry.get()
# if any of the entry field is empty then
if (d == '' or m == '' or y == ''
or gd == '' or gm == '' or gy == ''):
# show the error message
messagebox.showerror(title="Empty box Error",
message="Fill all Text fields")
# calling the clearAll function
clearAll()
return -1
elif d.isalpha():
# show the error message
messagebox.showerror(title="Empty box Error",
message="Fill all Text fields")
# calling the clearAll function
clearAll()
return -1
# function to calculate age
def resultant():
# check for error
value = checkError()
# if error is occur then return
if value == -1:
return
else:
# get the value from the respective entry boxes
birth_day = int(day_entry.get())
birth_month = int(month_entry.get())
birth_year = int(year_entry.get())
given_day = int(given_day_entry.get())
given_month = int(given_month_entry.get())
given_year = int(given_year_entry.get())
# convert the accessed birth data and given data in date format using date function of datetime module
date_of_birth = date(birth_year, birth_month, birth_day)
given_date = date(given_year, given_month, given_day)
# calculate the difference between two dates using relativedata function
result = relativedelta(given_date, date_of_birth)
# get the value of date, month and year from calculated result
year_from_result = result.years
month_from_result = result.months
day_from_result = result.days
# send the result data into respective result label
years_value.config(text=year_from_result)
months_value.config(text=month_from_result)
days_value.config(text=day_from_result)
# Heading Age calculator
heading = Label(root, text='Age Calculator', font=(
'Times new roman', 50, 'bold'), bg="#3a5a40")
heading.place(x=20, y=20, width=660, height=110)
# frame1 which contains all user entry boxes
frame1 = Label(root, font=(
'Times new roman', 50, 'bold'), bg="#4a4e69")
frame1.place(x=20, y=150, width=660, height=170)
# Date of birth heading label
dob = Label(root, text="Date of Birth", bg='#283618', fg='white',
font=('Helvetica', 15, 'bold'))
dob.place(x=40, y=170, width=200, height=30)
# given date heading label
given_date = Label(root, text="Given Date", bg='#283618', fg='white',
font=('Helvetica', 15, 'bold'))
given_date.place(x=400, y=170, width=160, height=30)
# button to display today's date
today_button = Button(root, text="Today", bg='#283618', fg='#f1faee',
font=('Helvetica', 14), border=4, relief='raised', command=today)
today_button.place(x=570, y=170, width=90, height=30)
# day label
day = Label(root, text="Day:", bg='#4a4e69', fg='black',
font=('lucida ', 15))
day.place(x=40, y=210, )
# month label
month = Label(root, text="Month:", bg='#4a4e69', fg='black',
font=('lucida ', 15))
month.place(x=40, y=240, )
# Year label
year = Label(root, text="Year:", bg='#4a4e69', fg='black',
font=('lucida ', 15))
year.place(x=40, y=270)
# Date of birth Entry Field
day_entry = Entry(root, text="", font=('arial', 15), bg='#ffe5d9')
day_entry.place(x=120, y=210, height=25, width=120)
month_entry = Entry(root, font=('arial', 15), bg='#ffe5d9')
month_entry.place(x=120, y=240, height=25, width=120)
year_entry = Entry(root, font=('arial', 15), bg='#ffe5d9')
year_entry.place(x=120, y=270, height=25, width=120)
# Given date Label
given_day = Label(root, text="Given Day:", bg='#4a4e69', fg='black',
font=('lucida ', 15))
given_day.place(x=400, y=210, )
given_month = Label(root, text="Given Month:", bg='#4a4e69', fg='black',
font=('lucida ', 15))
given_month.place(x=400, y=240, )
# Year label
given_year = Label(root, text="Given Year:", bg='#4a4e69', fg='black',
font=('lucida ', 15))
given_year.place(x=400, y=270)
# Given date entry field
given_day_entry = Entry(root, font=('arial', 15), bg='#ffe5d9')
given_day_entry.place(x=540, y=210, height=25, width=120)
given_month_entry = Entry(root, font=('arial', 15), bg='#ffe5d9')
given_month_entry.place(x=540, y=240, height=25, width=120)
given_year_entry = Entry(root, font=('arial', 15), bg='#ffe5d9')
given_year_entry.place(x=540, y=270, height=25, width=120)
# frame2 which contains results
frame2 = Label(root, font=(
'Times new roman', 50, 'bold'), bg="#4a4e69")
frame2.place(x=20, y=340, width=660, height=200)
# Resultant age button
resultant_age = Button(root, text="Resultant Age", command=resultant,
font=('arial', 15, 'bold'), bg='#22223b', fg='white')
resultant_age.place(x=40, y=360, width=495)
# Clear All Button
clear_all = Button(root, text="Clear All", command=clearAll,
font=('arial', 17, 'bold'), bg='#22223b', fg='red')
clear_all.place(x=545, y=360, width=115, height=40)
# Result Labels of years, months and days
years = Label(root, text="Years", bg='#0c1821', fg='white',
font=('Helvetica', 14,))
years.place(x=200, y=420, width=80, height=35)
months = Label(root, text="Months", bg='#0c1821', fg='white',
font=('Helvetica', 14,))
months.place(x=300, y=420, width=80, height=35)
days = Label(root, text="Days", bg='#0c1821', fg='white',
font=('Helvetica', 14,))
days.place(x=400, y=420, width=80, height=35)
# Result values of years, months and days
years_value = Label(root, text="", bg='#4a4e69', fg='white',
font=('Helvetica', 35, 'bold'))
years_value.place(x=200, y=470, width=80, height=35)
months_value = Label(root, text="", bg='#4a4e69', fg='white',
font=('Helvetica', 35, 'bold'))
months_value.place(x=300, y=470, width=80, height=35)
days_value = Label(root, text="", bg='#4a4e69', fg='white',
font=('Helvetica', 35, 'bold'))
days_value.place(x=400, y=470, width=80, height=35)
# functions for friends button
def aman():
clearAll()
year_entry.insert(0, "1996")
month_entry.insert(0, "08")
day_entry.insert(0, "05")
today()
resultant()
def ananya():
clearAll()
year_entry.insert(0, "1997")
month_entry.insert(0, "01")
day_entry.insert(0, "09")
today()
resultant()
def ashish():
clearAll()
year_entry.insert(0, "1997")
month_entry.insert(0, "11")
day_entry.insert(0, "18")
today()
resultant()
def ayush():
clearAll()
year_entry.insert(0, "1996")
month_entry.insert(0, "06")
day_entry.insert(0, "16")
today()
resultant()
def prashant():
clearAll()
year_entry.insert(0, "1997")
month_entry.insert(0, "03")
day_entry.insert(0, "02")
today()
resultant()
def priyanshi():
clearAll()
year_entry.insert(0, "1997")
month_entry.insert(0, "12")
day_entry.insert(0, "14")
today()
resultant()
def silver():
clearAll()
year_entry.insert(0, "1997")
month_entry.insert(0, "08")
day_entry.insert(0, "09")
today()
resultant()
def divya():
clearAll()
year_entry.insert(0, "1996")
month_entry.insert(0, "02")
day_entry.insert(0, "13")
today()
resultant()
# friends date of birth
aman = Button(root, text='Aman', bg='#03071e', fg='white', command=aman)
aman.place(x=35, y=560, width=75, height=30)
ananya = Button(root, text='Ananya', bg='#03071e', fg='white', command=ananya)
ananya.place(x=120, y=560, width=70, height=30)
ashish = Button(root, text='Ashish', bg='#03071e', fg='white', command=ashish)
ashish.place(x=200, y=560, width=70, height=30)
ayush = Button(root, text='Ayush', bg='#03071e', fg='white', command=ayush)
ayush.place(x=280, y=560, width=70, height=30)
prashant = Button(root, text='Prashant', bg='#03071e',
fg='white', command=prashant)
prashant.place(x=360, y=560, width=70, height=30)
priyanshi = Button(root, text='Priyanshi', bg='#03071e',
fg='white', command=priyanshi)
priyanshi.place(x=440, y=560, width=70, height=30)
silver = Button(root, text='Silver', bg='#03071e', fg='white', command=silver)
silver.place(x=520, y=560, width=70, height=30)
divya = Button(root, text='Divya', bg='#03071e', fg='white', command=divya)
divya.place(x=600, y=560, width=60, height=30)
# start the GUI app of tkinter
root.mainloop()