Introduction
In this Python tutorial, you will learn to create a Age calculator in python using the Tkinter library. It will calculate someone's age most accurately. For example, if the year of birth is a leap year and the date is 29th February and the current year is a leap year or not, the program will calculate the age correctly for any condition.
Users have to enter the date of birth, and the current date as an input through the tkinter widgets. The program will return the age in years, months, and days as an output.
⏰Visit Also: Create an Advanced Alarm Clock Using Python Tkinter
Requirements
Use pip3 instead of pip for Linux.
🔹Tkinter: pip install tk
🔹 DateTime: pip install DateTime
Read this before You proceed
You've to install the python Tkinter library to run this age calculator program. There is one more way to use this application. Just download the zip file of this python project from my GitHub page through the download button given below. The zip file contains the main python program file and executable files for Windows, and Linux both.
You can run the executable file directly without installing any third-party library.
The Code
'''Age calculator application in python'''
'''age_calculator.py'''
from tkinter import *
from tkinter import ttk
from datetime import date
from tkinter import messagebox
class Age_Calculator:
def __init__(self, root):
self.window = root
self.window.title("Age Calculator")
self.window.geometry("640x240+0+0")
self.month_list = ['January', 'February', 'March', 'April',
'May', 'June', 'July', 'August', 'September', 'October',
'November', 'December']
self.label1 = Label(self.window, text="Date of Birth", \
font=("times new roman",20,"bold")).place(x=90, y=30)
self.label2 = Label(self.window, text="Age at the date of", \
font=("times new roman",20,"bold")).place(x=40, y=80)
# Taking the birth month
self.month1 = StringVar()
self.month_combo = ttk.Combobox(self.window, width= 10, \
textvariable=self.month1)
self.month_combo['values'] = self.month_list
self.month_combo.current(0)
self.month_combo.place(x = 250, y = 35)
# Birth date
self.date1 = StringVar()
self.date_entry = Entry(self.window, \
textvariable=self.date1, width=10)
self.date_entry.insert(0, "1")
self.date_entry.place(x = 360, y = 35)
# Birth Year
self.year1 = StringVar()
self.year_entry = Entry(self.window, textvariable=self.year1, width=10)
self.year_entry.insert(0, "2000")
self.year_entry.place(x= 470, y = 35)
# Taking the current month
self.month2 = StringVar()
self.month2_combo = ttk.Combobox(self.window, width= 10,
textvariable=self.month2)
self.month2_combo['values'] = self.month_list
self.month2_combo.current(2)
self.month2_combo.place(x = 250, y = 87)
# Current Date
self.date2 = StringVar()
self.date2_entry = Entry(self.window,
textvariable=self.date2, width=10)
self.date2_entry.insert(0, "8")
self.date2_entry.place(x = 360, y = 87)
# Current Year
self.year2 = StringVar()
self.year2_entry = Entry(self.window,
textvariable=self.year2, width=10)
self.year2_entry.insert(0, "2022")
self.year2_entry.place(x= 470, y = 87)
self.calculate_button = Button(self.window, text="Calculate",\
bg="green", fg="white", font=("times new roman",12,"bold"),\
command=self.calculate_age).place(x = 270, y=150)
def LeapYear(self, year):
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return True
else:
return False
else:
return True
else:
return False
def NumOfDays(self, date1, date2):
return (date2 - date1).days
def calculate_age(self):
self.Months = {
1:31, 2:28, 3:31, 4:30, 5:31, 6:30, 7:31,
8:31, 9:30, 10:31, 11:30, 12:31}
self.month_sequence = {
"January":1, "February":2, "March":3, "April":4, "May":5,
"June":6, "July":7,"August":8, "September":9, "October":10,
"November":11, "December":12}
try:
day1 = int(self.date1.get())
mon1 = int(self.month_sequence[self.month_combo.get()])
year1= int(self.year1.get())
day2 = int(self.date2.get())
mon2 = int(self.month_sequence[self.month2_combo.get()])
year2= int(self.year2.get())
# Converting given dates into a date format
date1 = date(year1, mon1, day1)
date2 = date(year2, mon2, day2)
# Calculating age in days
TotalDays = self.NumOfDays(date1, date2)
# If Birth year and the current year is same
if year1 == year2:
month = TotalDays/30
day = TotalDays%30
year = 0
else:
year = TotalDays/365
month = (TotalDays%365)/30
# Check if the given year is a leap year or not.
# If Yes, then Make the total number of days in the month of
# February 29 instead of 28.
if self.LeapYear(year2):
self.Months[2] = 29
if day2 >= day1:
day = day2 - day1
# If the current month is February and the current year is leap
# year or not
elif mon2 == 2 & (self.LeapYear(year2) or \
(not self.LeapYear(year2))):
year = year
month = 11
# Checks the current month is January or Not
if mon2 == 1:
prevMonth = self.Months[mon2]
else:
prevMonth = self.Months[mon2 - 1]
days = prevMonth - day1 + day2
day = days
else:
if mon2 == 1:
prevMonth = self.Months[mon2]
else:
prevMonth = self.Months[mon2 - 1]
# Checks:
# 1. Birth Year is Leap year and
# 2. Birth Month is February and
# 3. Current Month is March or not
if self.LeapYear(year1) and (mon1 == 2) and (mon2 == 3):
days = prevMonth - day1 + day2 + 1
else:
days = prevMonth - day1 + day2
day = days
month = month
day = int(day)
month = int(month)
year = int(year)
messagebox.showinfo("Your Age", f"{year} years {month} \
months {day} days")
except Exception as es:
messagebox.showerror("Error!", f"Error due to {es}")
if __name__ == "__main__":
root = Tk()
obj = Age_Calculator(root)
root.mainloop()
Output
Making the python program into an executable file
It's a hesitating process to download a third-party module for executing a program. That's why I thought it was necessary to make an executable file for this python project. Here, I will show how to make a python program into an executable file for Windows and Linux both.
Do install PyInstaller at first. Use pip instead of pip3 for Windows.
☞pip3 install pyinstaller
Windows
Go to the command prompt and change the current working directory to the folder where the python file is. Then run this command.
☞pyinstaller --onefile -w age_calculator.py
Linux
Almost the same logic for Linux except for a small change in the command.
☞pyinstaller --onefile age_calculator.py
Download the source code and executable files
Download the zip file from my GitHub page(subhankar-rakshit) through the download button given below.
🔹Visit Also: Face Recognition in Python - Identify the faces of the Money Heist Characters
Conclusion
In this tutorial, you learned to create a GUI Age Calculator in Python. I used the Tkinter library to give this python project a pretty graphical user interface so that it becomes easier to use. It can calculate someone's age in years, months, and days accurately. In the end, we convert the python program file to an executable file.
I hope you loved this tutorial. Please share your love❤️ and drop your comment below. It would be an inspiration for me to create more lovely content in the future.
Thanks for reading!💙