Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c6f2f1c

Browse files
Added age calculator gui
1 parent da820e1 commit c6f2f1c

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

‎Age-Calculator-GUI/age_calc_gui.py‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# import libraries
2+
from tkinter import *
3+
from datetime import date
4+
5+
# initialized window
6+
root = Tk()
7+
root.geometry('280x300')
8+
root.resizable(0,0)
9+
root.title('Age Calculator')
10+
11+
# defining the function for calculating age
12+
def ageCalc():
13+
today = date.today()
14+
birthDate = date(int(yearEntry.get()), int(monthEntry.get()), int(dayEntry.get()))
15+
age = today.year - birthDate.year - ((today.day, today.month) < (birthDate.day, birthDate.month))
16+
statement = Label(text=f"{nameValue.get()}'s age is {age}.")
17+
statement.grid(row=6, column=1, pady=15)
18+
19+
# creating a label for person's name to display
20+
l1 = Label(text = "Name: ")
21+
l1.grid(row=1, column=0)
22+
nameValue = StringVar()
23+
24+
# creating a entry box for input
25+
nameEntry = Entry(root, textvariable=nameValue)
26+
nameEntry.grid(row=1, column=1, padx=10, pady=10)
27+
28+
# label for year in which user was born
29+
l2 = Label(text = "Year: ")
30+
l2.grid(row=2, column=0)
31+
yearValue = StringVar()
32+
yearEntry = Entry(root, textvariable=yearValue)
33+
yearEntry.grid(row=2, column=1, padx=10, pady=10)
34+
35+
# label for month in which user was born
36+
l3 = Label(text = "Month: ")
37+
l3.grid(row=3, column=0)
38+
monthValue = StringVar()
39+
monthEntry = Entry(root, textvariable=monthValue)
40+
monthEntry.grid(row=3, column=1, padx=10, pady=10)
41+
42+
# label for day/date on which user was born
43+
l4 = Label(text = "Day: ")
44+
l4.grid(row=4, column=0)
45+
dayValue = StringVar()
46+
dayEntry = Entry(root, textvariable=dayValue)
47+
dayEntry.grid(row=4, column=1, padx=10, pady=10)
48+
49+
# create a button for calculating age
50+
button = Button(text="Calculate age", command=ageCalc)
51+
button.grid(row=5, column=1)
52+
53+
# infinite loop to run program
54+
root.mainloop()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /