Age Calculator using Python

Hello Everyone ! In This session We are going to Create a simple Program " AGE CALCULATOR " using python 



I am using Tkinter Module (Graphical User Interface) .It is standard Toolkit used in python For GUI

Try this project if you want to make a simple Tkinter app that does some calculations in the background. It's an age calculator app that we can make relatively quickly and easily.

Users may enter their Date of birth into this age calculator app, and the app will automatically measure and show their Age. Isn't that fantastic?

LIBRARIES :

datetime  It helps to do operations on date and time .

ttk     : It is the extension of tkinter module , it enables many Themed widgets .

To begin the project, launch your code editor. First and foremost, we must include three libraries in our code.

The tkinter library is the first. Then, to deal with dates, we'll need the datetime library, Third one is ttk for Themed Widgets .

import datetime
import tkinter as tk
from tkinter import *
from tkinter import ttk 

WE are creating our app in Graphically so we need some colors , lets create a variable and assign the HEX VALUE of colour to it . 

c1='#99d6ff'  #sky blue color
c2='#3385ff' # light blue
c3='#ff9999' #light red

Lets Add some FONT to look better, Assigning font to variables 

font1='Times 27'
font2='Helvetica'

Let's start by making a SIMPLE WINDOW for our app and calling it Age Calculator App .By using  geometry () function we can decide the Height and Width of the window.

window=tk.Tk()
width= window.winfo_screenwidth()  
height= window.winfo_screenheight()   
window.geometry("%dx%d" % (width, height)) 
window.title(" Age Calculator App ")

We are creating 4 labels for Name , Year , Month , Date , and placing them in a correct order by using place() function.

name = tk.Label(text = " Enter  your  Name",width=15,height=2,background='#3385ff',foreground='white',font=font2)
name.place(x=530,y=150)
year = tk.Label(text = "Year",width=15,height=2,background='#3385ff',foreground='white',font=font2)
year.place(x=530,y=200)
month = tk.Label(text = "Month",width=15,height=2,background='#3385ff',foreground='white',font=font2)
month.place(x=530,y=250)
date = tk.Label(text = "Day",width=15,height=2,background='#3385ff',foreground='white',font=font2)
date.place(x=530,y=300)

We'll make entry fields to collect user inputs for all of the labels we've developed.

nameEntry = ttk.Entry(window , width=35,justify = CENTER)
nameEntry.place(x=700,y=150)
yearEntry = ttk.Entry(window , width=35,justify = CENTER)
yearEntry.place(x=700,y=200)
monthEntry = ttk.Entry(window , width=35,justify = CENTER)
monthEntry.place(x=700,y=250)
dateEntry = ttk.Entry(window , width=35,justify = CENTER)
dateEntry.place(x=700,y=300)

Then we'll define a function to collect user input. getInput () is the name of the feature .We'll build a Person class object within that function and transfer the name and birth date to the class's "__init__" method.

The int() method is used to convert values into integer format. Then, as output, we create a Text Area that displays the user's age. We'll make a button for users to submit their input values. The getInput feature is linked to the button.

def getInput():
    name=nameEntry.get()
    human = Person(name,datetime.date(int(yearEntry.get()),int(monthEntry.get()),int(dateEntry.get())))
    textArea = tk.Text(master=window,height=2,width=36,font=font1)
    textArea.place(x=355,y=490)
    answer = "  {human} !.  You are {age} years old ! ".format(human=name, age=human.age())
    textArea.insert(tk.END,answer)
button=tk.Button(window,text="Calculate Age",command=getInput,bg='#3385ff',fg ='white',height=1,width=20,font=font1)
button.place(x=520,y=400)

Let us now define the Person class. We will also code the __init__ method as well as the age method, which will calculate the user's age by subtracting the user's birth date from today's date.

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

I am adding one Label as a header which includes a heading "Age Calculator "to make it Colourful , Its your choice to add .

k=Label(window,background=c1,width=700,height=5).place(x=0,y=0)
k1=Label(window,text="A g e   C a l c u l a t o r",background=c1,foreground='white',font=font1).place(x=580,y=1)
k2=Label(window,background=c1,width=700,height=10).place(x=0,y=650)
k3=Label(window,background=c3,width=50,height=38).place(x=0,y=80)
k4=Label(window,background=c3,width=50,height=38).place(x=1010,y=80)

The mainloop() method in Atlast allows you to run everything within the window.

That is all there is to it. Here's a quick rundown of the entire code.

import datetime
import tkinter as tk
from tkinter import *
from tkinter import ttk 

c1='#99d6ff'#blue color
c2='#3385ff'#blue
c3='#ff9999'#red
font1='Times 27'#font
font2='Helvetica'

window=tk.Tk()
width= window.winfo_screenwidth()  
height= window.winfo_screenheight()   
window.geometry("%dx%d" % (width, height)) 
window.title(" Age Calculator App ")

name = tk.Label(text = " Enter  your  Name",width=15,height=2,background='#3385ff',foreground='white',font=font2)
name.place(x=530,y=150)
year = tk.Label(text = "Year",width=15,height=2,background='#3385ff',foreground='white',font=font2)
year.place(x=530,y=200)
month = tk.Label(text = "Month",width=15,height=2,background='#3385ff',foreground='white',font=font2)
month.place(x=530,y=250)
date = tk.Label(text = "Day",width=15,height=2,background='#3385ff',foreground='white',font=font2)
date.place(x=530,y=300)

nameEntry = ttk.Entry(window , width=35,justify = CENTER)
nameEntry.place(x=700,y=150)
yearEntry = ttk.Entry(window , width=35,justify = CENTER)
yearEntry.place(x=700,y=200)
monthEntry = ttk.Entry(window , width=35,justify = CENTER)
monthEntry.place(x=700,y=250)
dateEntry = ttk.Entry(window , width=35,justify = CENTER)
dateEntry.place(x=700,y=300)

def getInput():
    name=nameEntry.get()
    human = Person(name,datetime.date(int(yearEntry.get()),int(monthEntry.get()),int(dateEntry.get())))
    textArea = tk.Text(master=window,height=2,width=36,font=font1)
    textArea.place(x=355,y=490)
    answer = "  {human} !.  You are {age} years old ! ".format(human=name, age=human.age())
    textArea.insert(tk.END,answer)

button=tk.Button(window,text="Calculate Age",command=getInput,bg='#3385ff',fg ='white',height=1,width=20,font=font1)
button.place(x=520,y=400)

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
k=Label(window,background=c1,width=700,height=5).place(x=0,y=0)
k1=Label(window,text="A g e   C a l c u l a t o r",background=c1,foreground='white',font=font1).place(x=580,y=1)
k2=Label(window,background=c1,width=700,height=10).place(x=0,y=650)
k3=Label(window,background=c3,width=50,height=38).place(x=0,y=80)
k4=Label(window,background=c3,width=50,height=38).place(x=1010,y=80)

window.mainloop()

Finally our code is ready to Execute !!! Lets have a look how it is Executed 

After Inserting input the output will be like this !!


We used Python to create a fantastic-looking app, and I hope we did an excellent job !!Congratulations on the completion of your age calculator app!

If you have any questions or comments, please leave them in the comments section. I will gladly assist you.

It's now time to hone your programming skills. Try to create as many apps and projects as you can

Comments

Popular posts from this blog

How To Build Your Own Chatbot Using Python