Header Ads

How to create a age calculator using tkinter in Python?

Algorithm:


  1. Import the required modules: tkinter, datetime
  2. Create a tkinter window with a label and an entry box for the user to enter their birthdate.
  3. Create a button that when clicked, retrieves the birthdate from the entry box and calculates the age using the datetime module.
  4. Display the age in a label on the tkinter window.

Code:


import tkinter as tk
import datetime
from PIL import Image, ImageTk

window = tk.Tk()
window.geometry("520x680")
window.title("Age Calculator")

name = tk.Label(text = "Name")
name.grid(column=0, row=1)
year = tk.Label(text = "Year")
year.grid(column=0, row=2)
month = tk.Label(text = "Month")
month.grid(column=0, row=3)
date = tk.Label(text = "Day")
date.grid(column=0, row=4)

nameInput = tk.Entry()
nameInput.grid(column=1, row=1)
yearInput = tk.Entry()
yearInput.grid(column=1, row=2)
monthInput = tk.Entry()
monthInput.grid(column=1, row=3)
dateInput = tk.Entry()
dateInput.grid(column=1, row=4)

def get():
    name = nameInput.get()
    nam = Person(name, datetime.date(int(yearInput.get()),
                                     int(monthInput.get()),
                                     int(dateInput.get())))
    textArea = tk.Text(master=window, height=5, width=25)
    textArea.grid(column=1, row=6)
    answer = "Hi {nam}!. You are {age} years old !!"
                    .format(nam=name,age=nam.age())
    textArea.insert(tk.END, answer)
   
button = tk.Button(window, text="Calculate Age", command=get, bg="pink")
button.grid(column=1, row=5)

class Person:
    def __init__(self, name, birthdate):
        self.name = name
        self.birthdate = birthdate
    def age(self):
        today = datetime.date.today()
        age = today.year - self.birthdate.year
        return age
   
window.mainloop()


Source Code


Output:




Explanation:

This code is an implementation of a simple age calculator GUI using the Tkinter library in Python. The program allows the user to enter their name and birthdate, and then calculates their age when the "Calculate Age" button is clicked.
The program starts by importing necessary modules - tkinter and datetime, and PIL (Python Imaging Library) for image processing.
Next, a new window is created using the Tk() method, and its size and title are set. Then, the labels for the name, year, month, and date fields are created and placed on the grid using the grid() method. The corresponding entry fields are also created using the Entry() method and placed on the grid using the same method.
A function called "get()" is defined, which retrieves the user inputs and calculates the age using the Person class. It then displays the result using the Text() method and the insert() method.
Finally, a Button widget is created with the text "Calculate Age" and the command set to the "get()" function. The button is placed on the grid using the grid() method.
The Person class is defined with two attributes - name and birthdate, and a method called "age()" which calculates the age based on the birthdate provided.
The program then enters the main loop using the mainloop() method, which displays the window and waits for user input.
Overall, this code is a simple implementation of a GUI-based age calculator using the Tkinter library in Python.






Related Links:


Post a Comment

0 Comments