In this video I’ll show you how create a simple age calculator using Tkinter and Python!

In this app a user can enter the year they were born and the app will calculate their age.

Python Code: age.py
(Github Code)

from tkinter import *
from datetime import datetime
from tkinter import messagebox


root = Tk()
root.title('Codemy.com - Age Calculator')
root.iconbitmap('c:/gui/codemy.ico')
root.geometry("500x300")


def age():
	if my_entry.get():
		# Get the current year
		current_year = datetime.now().year
		# Calculate The Age
		your_age = current_year - int(my_entry.get())
		# Show age in message box
		messagebox.showinfo("Your Age", f"Your Age Is: {your_age}")

	else:
		# Show Error Message
		messagebox.showerror("Error", "You forgot to enter your age!")

my_label = Label(root, text="Enter Year Born", font=("Helvetica", 24))
my_label.pack(pady=20)

my_entry = Entry(root, font=("Helvetica", 18))
my_entry.pack(pady=20)

my_button = Button(root, text="Calculate Age!", font=("Helvetica", 18), command=age)
my_button.pack(pady=20)



root.mainloop()

John Elder

John is the CEO of Codemy.com where he teaches over 100,000 students how to code! He founded one of the Internet's earliest advertising networks and sold it to a publicly company at the height of the first dot com boom. After that he developed the award-winning Submission-Spider search engine submission software that's been used by over 3 million individuals, businesses, and governments in over 42 countries. He's written several Amazon #1 best selling books on coding, and runs a popular Youtube coding channel.

View all posts

Add comment

Your email address will not be published. Required fields are marked *

John Elder

John is the CEO of Codemy.com where he teaches over 100,000 students how to code! He founded one of the Internet's earliest advertising networks and sold it to a publicly company at the height of...