forked from Red-0111/Anything-Repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathage calculator.py
91 lines (67 loc) · 2.79 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
# importing the tkinter module
from tkinter import *
# importing date class from datetime module
from datetime import date
# initializing tkinter
root = Tk()
# setting the width and height of gui
root.geometry("700x500")
# setting the title of gui
root.title("Age Calculator")
# loading image
photo = PhotoImage(file="filename.png")
# creating a Label widget to show the image we load
myimage = Label(image=photo)
# placing the image Label widget using grid method
myimage.grid(row=0, column=1)
# defining function to calculate age according to the data given
# by the user
def calculateAge():
# storing today's date in "today" variable
today = date.today()
# getting birthdate using .get() method
birthDate = date(int(yearEntry.get()), int(monthEntry.get()), int(dayEntry.get()))
# calculating age by subtracting birthdate from today's date
age = today.year - birthDate.year - ((today.month, today.day) < (birthDate.month, birthDate.day))
# creating a Label widget to show the calculated age using
# grid method
Label(text=f"{nameValue.get()} your age is {age}").grid(row=6, column=1)
# creating a label widget for asking user his/her name
Label(text="Name").grid(row=1, column=0, padx=90)
# creating a label widget for asking user his/her year of birth
Label(text="Year").grid(row=2, column=0)
# creating a label widget for asking user his/her month of birth
Label(text="Month").grid(row=3, column=0)
# creating a label widget for asking user his/her day of birth
Label(text="Day").grid(row=4, column=0)
# declaring a variable of string datatype to store the name value
# entered by the user
nameValue = StringVar()
# declaring a variable of string datatype to store the year value
# entered by the user
yearValue = StringVar()
# declaring a variable of string datatype to store the month
# value entered by the user
monthValue = StringVar()
# declaring a variable of string datatype to store the day value
# entered by the user
dayValue = StringVar()
# creating an entry widget to take name value
nameEntry = Entry(root, textvariable=nameValue)
# creating an entry widget to take year value
yearEntry = Entry(root, textvariable=yearValue)
# creating an entry widget to take month value
monthEntry = Entry(root, textvariable=monthValue)
# creating an entry widget to take day value
dayEntry = Entry(root, textvariable=dayValue)
# placing the entry widgets
nameEntry.grid(row=1, column=1, pady=10)
yearEntry.grid(row=2, column=1, pady=10)
monthEntry.grid(row=3, column=1, pady=10)
dayEntry.grid(row=4, column=1, pady=10)
# creating and placing a button to calculate a show age on
# clicking on this button
Button(text="Calculate age", command=calculateAge).grid(row=5, column=1, pady=10)
# mainloop() is an infinite loop used to run the application when
# it's in ready state
root.mainloop()