Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
Creating an age calculator using Tkinter is a good exercise to understand the basics of GUI development along with some logical operations.
In this tutorial, you will learn how to build a simple age calculator that takes a user's birthdate and calculates their age.
import tkinter as tk from tkinter import ttk from datetime import datetime
root = tk.Tk() root.title("Age Calculator")
You can use ttk.Entry
widgets to get the date, month, and year of birth:
frame = ttk.LabelFrame(root, text="Enter your birthdate") frame.pack(pady=20) # Labels ttk.Label(frame, text="Year").grid(row=0, column=0) ttk.Label(frame, text="Month").grid(row=0, column=1) ttk.Label(frame, text="Day").grid(row=0, column=2) # Entry widgets year_var = tk.StringVar() month_var = tk.StringVar() day_var = tk.StringVar() year_entry = ttk.Entry(frame, textvariable=year_var, width=5) month_entry = ttk.Entry(frame, textvariable=month_var, width=5) day_entry = ttk.Entry(frame, textvariable=day_var, width=5) year_entry.grid(row=1, column=0, padx=10, pady=10) month_entry.grid(row=1, column=1, padx=10, pady=10) day_entry.grid(row=1, column=2, padx=10, pady=10)
To calculate age:
def calculate_age(): today = datetime.now() birthdate = datetime(int(year_var.get()), int(month_var.get()), int(day_var.get())) age = today.year - birthdate.year - ((today.month, today.day) < (birthdate.month, birthdate.day)) age_label.config(text=f"You are {age} years old!") calculate_button = ttk.Button(root, text="Calculate Age", command=calculate_age) calculate_button.pack(pady=20)
Create a label to display the age:
age_label = ttk.Label(root, text="") age_label.pack(pady=20)
import tkinter as tk from tkinter import ttk from datetime import datetime def calculate_age(): today = datetime.now() birthdate = datetime(int(year_var.get()), int(month_var.get()), int(day_var.get())) age = today.year - birthdate.year - ((today.month, today.day) < (birthdate.month, birthdate.day)) age_label.config(text=f"You are {age} years old!") root = tk.Tk() root.title("Age Calculator") frame = ttk.LabelFrame(root, text="Enter your birthdate") frame.pack(pady=20) # Labels ttk.Label(frame, text="Year").grid(row=0, column=0) ttk.Label(frame, text="Month").grid(row=0, column=1) ttk.Label(frame, text="Day").grid(row=0, column=2) # Entry widgets year_var = tk.StringVar() month_var = tk.StringVar() day_var = tk.StringVar() year_entry = ttk.Entry(frame, textvariable=year_var, width=5) month_entry = ttk.Entry(frame, textvariable=month_var, width=5) day_entry = ttk.Entry(frame, textvariable=day_var, width=5) year_entry.grid(row=1, column=0, padx=10, pady=10) month_entry.grid(row=1, column=1, padx=10, pady=10) day_entry.grid(row=1, column=2, padx=10, pady=10) calculate_button = ttk.Button(root, text="Calculate Age", command=calculate_age) calculate_button.pack(pady=20) age_label = ttk.Label(root, text="") age_label.pack(pady=20) root.mainloop()
Now, running this code will provide you with a simple age calculator where you can input your birthdate and get your age.
Python Tkinter date of birth calculator example:
import tkinter as tk from datetime import datetime def calculate_age(): birth_date_str = entry_birth_date.get() try: birth_date = datetime.strptime(birth_date_str, "%Y-%m-%d") today = datetime.now() age = today.year - birth_date.year - ((today.month, today.day) < (birth_date.month, birth_date.day)) label_result.config(text=f"Age: {age} years") except ValueError: label_result.config(text="Invalid date format") root = tk.Tk() root.title("Age Calculator") label_instructions = tk.Label(root, text="Enter your date of birth (YYYY-MM-DD):") label_instructions.pack(pady=10) entry_birth_date = tk.Entry(root) entry_birth_date.pack(pady=10) button_calculate = tk.Button(root, text="Calculate Age", command=calculate_age) button_calculate.pack(pady=10) label_result = tk.Label(root, text="") label_result.pack(pady=10) root.mainloop()
Tkinter date picker for Age Calculator:
import tkinter as tk from tkinter import ttk from tkcalendar import DateEntry # Install this module using: pip install tkcalendar def calculate_age(): birth_date = entry_birth_date.get_date() today = datetime.now() age = today.year - birth_date.year - ((today.month, today.day) < (birth_date.month, birth_date.day)) label_result.config(text=f"Age: {age} years") root = tk.Tk() root.title("Age Calculator with Date Picker") label_instructions = tk.Label(root, text="Select your date of birth:") label_instructions.pack(pady=10) entry_birth_date = DateEntry(root, width=12, background="darkblue", foreground="white", borderwidth=2) entry_birth_date.pack(pady=10) button_calculate = tk.Button(root, text="Calculate Age", command=calculate_age) button_calculate.pack(pady=10) label_result = tk.Label(root, text="") label_result.pack(pady=10) root.mainloop()
Age calculation with entry widgets in Tkinter:
import tkinter as tk from datetime import datetime def calculate_age(): try: birth_year = int(entry_year.get()) birth_month = int(entry_month.get()) birth_day = int(entry_day.get()) birth_date = datetime(birth_year, birth_month, birth_day) today = datetime.now() age = today.year - birth_date.year - ((today.month, today.day) < (birth_date.month, birth_date.day)) label_result.config(text=f"Age: {age} years") except ValueError: label_result.config(text="Invalid date components") root = tk.Tk() root.title("Age Calculator with Entry Widgets") label_instructions = tk.Label(root, text="Enter your date of birth:") label_instructions.pack(pady=10) entry_year = tk.Entry(root, width=4) entry_year.pack(side=tk.LEFT, padx=5) label_year = tk.Label(root, text="Year") label_year.pack(side=tk.LEFT) entry_month = tk.Entry(root, width=2) entry_month.pack(side=tk.LEFT, padx=5) label_month = tk.Label(root, text="Month") label_month.pack(side=tk.LEFT) entry_day = tk.Entry(root, width=2) entry_day.pack(side=tk.LEFT, padx=5) label_day = tk.Label(root, text="Day") label_day.pack(side=tk.LEFT) button_calculate = tk.Button(root, text="Calculate Age", command=calculate_age) button_calculate.pack(pady=10) label_result = tk.Label(root, text="") label_result.pack(pady=10) root.mainloop()