Python Projects

GUI Age Calculator with Python


Python age calculator with Tkinter

This tutorial teaches you how to develop a visual age GUI calculator with Python.

The code requires basic understanding of Python coding (including the built-in library datetime) as well as Tkinter (check here for more).

If you are not familiar with Python coding, please check these tutorials first.

Let’s start.

We use Python version 3.10+ and PyCharm (IDE).

Plan

This calculator contains 3 main parts:

  1. Age calculation function
  2. Main configuration
  3. Visual graphics and structuring

The calculator asks the user to input a birthday day, month, and year. As an outcome, it calculates how old a person is by taking the time of the calculation and the birthday date into account. The output is displayed in years.

Age calculation function

We need to import the built-in datetime libraries first.

from datetime import date
from tkinter import *

After this, we create a function which calculates the the difference between the time of the calculation and the birthday date.

The function is straightforward. It takes into account today’s day, month, and year. Furthermore, it calculates the difference in time, stores it in a variable (age) and finally returns the result.

def calculation():
    today = date.today()
    d = int(d_entry.get())
    m = int(m_entry.get())
    y = int(y_entry.get())
    bd = date(y, m, d)
    age = today.year - bd.year - ((today.month, today.day) < (bd.month, bd.day))
    result.config(text='The age is ' + str(age))

Main configuration

Within this section, we configure the main window setup.

This section is short and easy. First, we test for script execution. After this, we configure the window features (such as size and colour).

Here, we also choose the layout of the program. In this instance, we use pack (as opposed to a grid or place layout).

if __name__ == "__main__":

    calc = Tk()

    calc.title('Age GUI Calculator')
    calc.geometry('400x350')
    calc.configure(bg="silver")

    canvas = Canvas(calc, width=400, height=350)
    canvas.pack()

Visual graphics and structuring

The this final part, we set up all GUI visuals for the age calculator. This includes button, entry box, and result allocation

This part is quite straightforward, it’s mainly copy and paste (with some adjusting until you get the desired display outcome).

    title = Label(calc, text='Age GUI Calculator', font="Ariel 21")

    canvas.create_window(200, 40, window=title)


    d_label = Label(calc, text='Day:', font="Ariel 12")
    d_entry = Entry(calc, width=15)

    m_label = Label(calc, text='Month:', font="Ariel 12")
    m_entry = Entry(calc, width=15)

    y_label = Label(calc, text='Year:', font="Ariel 12")
    y_entry = Entry(calc, width=15)

    calculation_button = Button(calc, text='Calculate', command=calculation)

    result = Label(calc, text='', font="Ariel 12")

    canvas.create_window(200, 100, window=d_label)
    canvas.create_window(200, 130, window=d_entry)

    canvas.create_window(200, 160, window=m_label)
    canvas.create_window(200, 190, window=m_entry)

    canvas.create_window(200, 220, window=y_label)
    canvas.create_window(200, 250, window=y_entry)

    canvas.create_window(200, 290, window=calculation_button)

    canvas.create_window(200, 330, window=result)


    calc.mainloop()

Example

Below is an example of the execution of the visual age GUI calculator.

visual-age-gui-calculator-python-example

Full code of Age Calculator

Below is the full Python code of the visual age GUI calculator.

from datetime import date
from tkinter import *


def calculation():
    today = date.today()
    d = int(d_entry.get())
    m = int(m_entry.get())
    y = int(y_entry.get())
    bd = date(y, m, d)
    age = today.year - bd.year - ((today.month, today.day) < (bd.month, bd.day))
    result.config(text='The age is ' + str(age))


if __name__ == "__main__":

    calc = Tk()

    calc.title('Age GUI Calculator')
    calc.geometry('400x350')
    calc.configure(bg="silver")

    canvas = Canvas(calc, width=400, height=350)
    canvas.pack()


    title = Label(calc, text='Age GUI Calculator', font="Ariel 21")

    canvas.create_window(200, 40, window=title)


    d_label = Label(calc, text='Day:', font="Ariel 12")
    d_entry = Entry(calc, width=15)

    m_label = Label(calc, text='Month:', font="Ariel 12")
    m_entry = Entry(calc, width=15)

    y_label = Label(calc, text='Year:', font="Ariel 12")
    y_entry = Entry(calc, width=15)

    calculation_button = Button(calc, text='Calculate', command=calculation)

    result = Label(calc, text='', font="Ariel 12")

    canvas.create_window(200, 100, window=d_label)
    canvas.create_window(200, 130, window=d_entry)

    canvas.create_window(200, 160, window=m_label)
    canvas.create_window(200, 190, window=m_entry)

    canvas.create_window(200, 220, window=y_label)
    canvas.create_window(200, 250, window=y_entry)

    canvas.create_window(200, 290, window=calculation_button)

    canvas.create_window(200, 330, window=result)


    calc.mainloop()

Next: Password Generator with Python

by AICorr Team

We are proud to offer our extensive knowledge to you, for free. The AICorr Team puts a lot of effort in researching, testing, and writing the content within the platform (aicorr.com). We hope that you learn and progress forward.