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 e673ab3

Browse files
Merge pull request avinashkranjan#808 from mehabhalodiya/agecalc
Added Age Calculator GUI
2 parents 794e3e3 + 5f70125 commit e673ab3

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

‎Age-Calculator-GUI/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Age Calculator GUI
2+
3+
In this age calculator app, users can type in their date of birth, and the app will calculate and display their age.
4+
5+
## Step 1:
6+
7+
First of all, we need to import two libraries into our code. The first one is the *tkinter* library. Then, we need the *datetime* library to work with dates.
8+
9+
## Step 2:
10+
11+
Now, let’s create a simple window for our app and name it as *Age Calculator*.
12+
13+
## Step 3:
14+
15+
Then, we are going to create four labels, each for the name, year, month, and the date, and put them in the grid.
16+
17+
We will create entry fields to get the user inputs corresponding to all the labels created. Put them at the right side of the corresponding labels using the *grid* method.
18+
19+
## Step 4:
20+
21+
Then, we are going to define a function, which will calculate the age of the user by subtracting the user’s birth date from today’s date. We name that function as `ageCalc()`.
22+
23+
Then, we create a `Label` area that will display the age of the user as output in the function itself.
24+
25+
## Step 5:
26+
27+
Then, we are going to create a button for users to submit their input values. We link the button to the `ageCalc` function.
28+
29+
Finally, let’s run everything inside the window using the `mainloop()` method.

‎Age-Calculator-GUI/age_calc_gui.py

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

0 commit comments

Comments
(0)

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