In this tutorial, we will learn about how to create s Python Tkinter Age Calculator in Python. In this application, we will see the user can enter their date of birth and the calculator automatically calculate the age of the person. How old the person just simply type their date of birth and the calculated tell the exact age.
Python Tkinter age calculator
Block of code:
In this block of Python Tkinter Age Calculator block of code, we simply import some libraries from which we can create an application Age Calculator.
With the help of this application, the calculator can calculate the get just simply typing the date of birth in the entry box.
from tkinter import *
import datetime
import tkinter as tk
from PIL import Image,ImageTk
Block of code:
In this Python Tkinter Age Calculator block of code, we will create a workspace where the calculator can calculate the age of the Person.
- wd.geometry(“400×400”) is used to give the width and height to the window.
- wd.title(“Pythontpoint”) is used to give a title to the window.
- nameofperson = tk.Label(text = “Name”) is used to give label as name.
- nameofperson.grid(column=0,row=1) is used to create the grid for name.
- year = tk.Label(text = “Year”) is used to give a label as year.
- year.grid(column=0,row=2) is used to create the grid for the year.
- date = tk.Label(text = “Day”) is used to give a date as a label.
- date.grid(column=0,row=4) is used to create a grid for date.
- nameofpersonEntry = tk.Entry() is used to create the entry box for name.
- nameofpersonEntry.grid(column=1,row=1) is used is used to create the entry box grid.
- yearentry = tk.Entry() is used to create the year entry box.
- yearentry.grid(column=1,row=2) is used to create the year entry grid.
- monthentry = tk.Entry() is used to create the month entry.
- monthentry.grid(column=1,row=3) is used to create month entry grid.
- dateentry = tk.Entry() is used to create the date entry box.
- dateentry.grid(column=1,row=4) is used to create the date entry grid.
wd=tk.Tk()
wd.geometry("400x400")
wd.title("Pythontpoint")
nameofperson = tk.Label(text = "Name")
nameofperson.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)
nameofpersonEntry = tk.Entry()
nameofpersonEntry.grid(column=1,row=1)
yearentry = tk.Entry()
yearentry.grid(column=1,row=2)
monthentry = tk.Entry()
monthentry.grid(column=1,row=3)
dateentry = tk.Entry()
dateentry.grid(column=1,row=4)
Block of code:
In this Python Tkinter Age Calculator block of code, we will create an entry box for all the labels in which users can enter the input.
- nameofperson=nameofpersonEntry.get() is used to get the name as the input from the user.
- dog=Person(nameofperson,datetime.date(int(yearentry.get()),int(monthentry.get()),int(dateentry.get()))) is used to get the month as a input from the user.
- textArea = tk.Text(master=wd,height=10,width=25) is used where the text is created.
- textArea.grid(column=1,row=6) is used as a text grid.
- textArea.insert(tk.END,answer) is used to insert in the text area.
- button=tk.Button(wd,text=”Calculate Age Of Person”,command=getinput,bg=”cyan”) is used to create a button on the screen.
- self.nameofperson = nameofperson is used to enter their own name.
- self.birthdate = birthdate is used to enter their own date of birth.
- today = datetime.date.today() is used to enter the today date.
- age = today.year-self.birthdate.year is used to enter the birthday date.
def getinput():
nameofperson=nameofpersonEntry.get()
dog = Person(nameofperson,datetime.date(int(yearentry.get()),int(monthentry.get()),int(dateentry.get())))
textArea = tk.Text(master=wd,height=10,width=25)
textArea.grid(column=1,row=6)
answer = " Heyya {dog}!!. You are {age} years old!!! ".format(dog=nameofperson, age=dog.age())
textArea.insert(tk.END,answer)
button=tk.Button(wd,text="Calculate Age Of Person",command=getinput,bg="cyan")
button.grid(column=1,row=5)
class Person:
def __init__(self,nameofperson,birthdate):
self.nameofperson = nameofperson
self.birthdate = birthdate
def age(self):
today = datetime.date.today()
age = today.year-self.birthdate.year
return age
Read: Python Tkinter Color Game
Block of code:
In this Python Tkinter Age Calculator block of code, we will place the image above all the labels which look attractive on the screen.
- image=Image.open(‘calculator.jpg’) is used to place the image on the screen.
- photo=ImageTk.PhotoImage(image) is used to set the image on the screen.
- labelimage=tk.Label(image=photo) is used to give the label as an image.
- labelimage.grid(column=1,row=0) is used to create the image grid.
image=Image.open('calculator.jpg')
image.thumbnail((200,500),Image.ANTIALIAS)
photo=ImageTk.PhotoImage(image)
labelimage=tk.Label(image=photo)
labelimage.grid(column=1,row=0)
wd.mainloop()
Github Link
Check this code in Repository from Github and you can also fork this code.
Github User Name: PythonT-Point
Code:
from tkinter import *
import datetime
import tkinter as tk
from PIL import Image,ImageTk
wd=tk.Tk()
wd.geometry("400x400")
wd.title("Pythontpoint")
nameofperson = tk.Label(text = "Name")
nameofperson.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)
nameofpersonEntry = tk.Entry()
nameofpersonEntry.grid(column=1,row=1)
yearentry = tk.Entry()
yearentry.grid(column=1,row=2)
monthentry = tk.Entry()
monthentry.grid(column=1,row=3)
dateentry = tk.Entry()
dateentry.grid(column=1,row=4)
def getinput():
nameofperson=nameofpersonEntry.get()
dog = Person(nameofperson,datetime.date(int(yearentry.get()),int(monthentry.get()),int(dateentry.get())))
textArea = tk.Text(master=wd,height=10,width=25)
textArea.grid(column=1,row=6)
answer = " Heyya {dog}!!. You are {age} years old!!! ".format(dog=nameofperson, age=dog.age())
textArea.insert(tk.END,answer)
button=tk.Button(wd,text="Calculate Age Of Person",command=getinput,bg="cyan")
button.grid(column=1,row=5)
class Person:
def __init__(self,nameofperson,birthdate):
self.nameofperson = nameofperson
self.birthdate = birthdate
def age(self):
today = datetime.date.today()
age = today.year-self.birthdate.year
return age
image=Image.open('calculator.jpg')
image.thumbnail((200,500),Image.ANTIALIAS)
photo=ImageTk.PhotoImage(image)
labelimage=tk.Label(image=photo)
labelimage.grid(column=1,row=0)
wd.mainloop()
Output:
After running the above code we get the following output in which we can see that the labels and grid are placed on the screen where the user can enter the input according to the label and the calculated can calculate accordingly.
After entering the input in the entry box click on the Calculate Age Of Person button and the output is shown in the text box grid.
So, in this tutorial, we discussed Python Tkinter Age Calculator and we have explained this with the help of the example that you can see above.
I?¦ve read several good stuff here. Certainly price bookmarking for revisiting. I surprise how much attempt you place to create one of these fantastic informative site.
Hey very cool website!! Man .. Excellent .. Amazing .. I’ll bookmark your web site and take the feeds additionally?KI’m happy to find so many useful info here in the put up, we want work out more strategies in this regard, thanks for sharing. . . . . .
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
Your article helped me a lot, is there any more related content? Thanks!
I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.
Your point of view caught my eye and was very interesting. Thanks. I have a question for you. https://www.binance.info/it/join?ref=S5H7X3LP
I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article. https://accounts.binance.com/zh-CN/register?ref=VDVEQ78S