Movatterモバイル変換


[0]ホーム

URL:


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

Commitc4508bc

Browse files
committed
add age calculator tutorial
1 parentf7dbba5 commitc4508bc

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,5 +240,6 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
240240
-[How to Build a Spreadsheet App with Tkinter in Python](https://www.thepythoncode.com/article/spreadsheet-app-using-tkinter-in-python). ([code](gui-programming/spreadsheet-app))
241241
-[How to Make a Rich Text Editor with Tkinter in Python](https://www.thepythoncode.com/article/create-rich-text-editor-with-tkinter-python). ([code](gui-programming/rich-text-editor))
242242
-[How to Make a Python Code Editor using Tkinter in Python](https://www.thepythoncode.com/article/python-code-editor-using-tkinter-python). ([code](gui-programming/python-code-editor/))
243+
-[How to Make an Age Calculator in Python](https://www.thepythoncode.com/article/age-calculator-using-tkinter-python). ([code](gui-programming/age-calculator))
243244

244245
For any feedback, please consider pulling requests.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#[How to Make an Age Calculator in Python](https://www.thepythoncode.com/article/age-calculator-using-tkinter-python)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
fromdatetimeimportdate
2+
3+
defcalculate_age(day,month,year):
4+
# we are getting the current date using the today()
5+
today=date.today()
6+
# convering year, month and day into birthdate
7+
birthdate=date(year,month,day)
8+
# calculating the age
9+
age=today.year-birthdate.year- ((today.month,today.day)< (birthdate.month,birthdate.day))
10+
# return the age value
11+
returnage
12+
13+
14+
# the try/except block
15+
# the try will execute if there are no exceptions
16+
try:
17+
# we are getting day, month and year using input() function
18+
day=input('Enter day:')
19+
month=input('Enter month:')
20+
year=input('Enter year:')
21+
# creating a variable called calculated_age and we are also calling the claculate_age function
22+
age_result=calculate_age(int(day),int(month),int(year))
23+
print(f'You are{age_result} years old')
24+
25+
# the except will catch all errors
26+
except:
27+
print(f'Failed to calculate age, either day or month or year is invalid')
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
fromtkinterimport*
2+
fromtkinterimportttk
3+
fromdatetimeimportdate
4+
fromtkinter.messageboximportshowerror
5+
6+
7+
# the function for calculating the age
8+
defcalculate_age():
9+
# the try/except block
10+
try:
11+
# getting current date
12+
today=date.today()
13+
# getting day from the day entry
14+
day=int(day_entry.get())
15+
# getting month from the month entry
16+
month=int(month_entry.get())
17+
# getting year from the year entry
18+
year=int(year_entry.get())
19+
# creating a date object
20+
birthdate=date(year,month,day)
21+
# calculating the age
22+
age=today.year-birthdate.year- ((today.month,today.day)< (birthdate.month,birthdate.day))
23+
# displaying the age using the age result label
24+
age_result.config(text='You are '+str(age)+' years old')
25+
# if an error occurs the showerror window will pop up
26+
except:
27+
showerror(title='Error',message='An error occurred while trying to ' \
28+
'calculate age\nThe following could ' \
29+
'be the causes:\n->Invalid input data\n->An empty field/fields\n'\
30+
'Make sure you enter valid data and fill all the fields')
31+
32+
33+
34+
# creating the main window
35+
window=Tk()
36+
# the title for the window
37+
window.title('Age Calculator')
38+
# the dimensions and position of the windodw
39+
window.geometry('500x260+430+300')
40+
# making the window nonresizabale
41+
window.resizable(height=FALSE,width=FALSE)
42+
43+
# the canvas to contain all the widgets
44+
canvas=Canvas(window,width=500,height=400)
45+
canvas.pack()
46+
47+
# ttk styles for the labels
48+
label_style=ttk.Style()
49+
label_style.configure('TLabel',foreground='#000000',font=('OCR A Extended',14))
50+
51+
# ttk styles for the button
52+
button_style=ttk.Style()
53+
button_style.configure('TButton',foreground='#000000',font=('DotumChe',16))
54+
55+
# ttk styles for the entries
56+
entry_style=ttk.Style()
57+
entry_style.configure('TEntry',font=('Dotum',15))
58+
59+
# the label for displaying the big text
60+
big_label=Label(window,text='AGE CALCULATOR',font=('OCR A Extended',25))
61+
62+
# placing the big label inside the canvas
63+
canvas.create_window(245,40,window=big_label)
64+
65+
66+
# label and entry for the day
67+
day_label=ttk.Label(window,text='Day:',style='TLabel')
68+
day_entry=ttk.Entry(window,width=15,style='TEntry')
69+
70+
# label and entry for the month
71+
month_label=ttk.Label(window,text='Month:',style='TLabel')
72+
month_entry=ttk.Entry(window,width=15,style='TEntry')
73+
74+
# label and entry for the year
75+
year_label=ttk.Label(window,text='Year:',style='TLabel')
76+
year_entry=ttk.Entry(window,width=15,style='TEntry')
77+
78+
# the button
79+
calculate_button=ttk.Button(window,text='Calculate Age',style='TButton',command=calculate_age)
80+
81+
# label for display the calculated age
82+
age_result=ttk.Label(window,text='',style='TLabel')
83+
84+
85+
# adding the day label and entry inside the canvas
86+
canvas.create_window(114,100,window=day_label)
87+
canvas.create_window(130,130,window=day_entry)
88+
89+
# adding the month label and entry inside the canvas
90+
canvas.create_window(250,100,window=month_label)
91+
canvas.create_window(245,130,window=month_entry)
92+
93+
# adding the year label and entry inside the canvas
94+
canvas.create_window(350,100,window=year_label)
95+
canvas.create_window(360,130,window=year_entry)
96+
97+
# adding the age_result and entry inside the canvas
98+
canvas.create_window(245,180,window=age_result)
99+
100+
# adding the calculate button inside the canvas
101+
canvas.create_window(245,220,window=calculate_button)
102+
103+
104+
# runs the window infinitely until uses closes it
105+
window.mainloop()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp