GUI Calculator In Python

GUI Calculator Using Tkinter in Python

Python

GUI Calculator Using Tkinter in Python – In This Tutorial We are going to create Python Tkinter GUI Calculator With Step By Step And WIth Explanation.

Modules needed for this program.
1. Tkinter – For making a GUI window

Importing all modules for this program.

from tkinter import *

Creating a plane GUI(Graphical User Interface) window with the help of tkinter module.

For creating a plane GUI window you just have to type two lines of code. We will do all the coding in between of these two lines of code so that our code is shown in the GUI window.

from tkinter import *

window = Tk()


window.mainloop()
This is our basic GUI window

Giving size and title to our GUI window.

For giving size to our GUI window we will use:-

  1. window.minsize(width, height) – This is for setting the minimum width and height for our window
  2. window.maxsize(width, height) – This is for setting the maximum width and height for our window
  3. window.title(‘Title here’) – This is for giving title to our window
from tkinter import *

window = Tk()
window.minsize(600,400)
window.maxsize(600,400)
window.title('Calculator')

window.mainloop()
This is the window after giving size and title to it.

Giving a heading to our window.

For creating a heading for our window we have to create a label and then inserting the text in that label. Label is a widget which is just for show and user can not interact with it.

inside Label widget we have to give the following arguments :- 
1.  text (For the text shown in that label)
2. font (For adding font to the text in that label)
3. bg(For adding a backgroud color to the label)
from tkinter import *

window = Tk()
window.minsize(600,400)
window.maxsize(600,400)
window.title('Calculator')

heading = Label(window,text='Calculator',font=('bold',50),bg='cyan')
heading.pack(fill=X)

window.mainloop()
This is how our window is looking after adding heading.

Making three Entry tags for taking inputs from the users.
For taking inputs from the user we will be making three different Entry tags.

  1. First Entry tag is for taking first number from the user.
  2. Second Entry tag is for taking the operational value from the user.
  3. Third Entry tag is for taking second number from the user

For placing these all Entry tags we will be using .place() and inside .place() we will give the direction where the Entry tag is placed though x-axis ans y-axis

from tkinter import *

window = Tk()
window.minsize(600,400)
window.maxsize(600,400)
window.title('Calculator')

heading = Label(window,text='Calculator',font=('bold',50),bg='cyan')
heading.pack(fill=X)

num_1 = Entry(window,bg='light cyan',font=('normal',30),width=5)  #  for taking first number from the user.
num_1.place(x=140,y=100)

operation = Entry(window,bg='light cyan',font=('normal',30),width=1) #  for taking the operational value from the user.
operation.place(x=290,y=100)

num_2 = Entry(window,bg='light cyan',font=('normal',30),width=5) # for taking second number from the user
num_2.place(x=360,y=100)

window.mainloop()

All three Entry tags are placed inside the window

Creating a blank function.
We will create a function and will type “pass” in that function so that we get a blank function. This has nothing inside it but it will not give an error while running it.

from tkinter import *

window = Tk()
window.minsize(600,400)
window.maxsize(600,400)
window.title('Calculator')

def main_function():
    pass

heading = Label(window,text='Calculator',font=('bold',50),bg='cyan')
heading.pack(fill=X)

num_1 = Entry(window,bg='light cyan',font=('normal',30),width=5)
num_1.place(x=140,y=100)

operation = Entry(window,bg='light cyan',font=('normal',30),width=1)
operation.place(x=290,y=100)

num_2 = Entry(window,bg='light cyan',font=('normal',30),width=5)
num_2.place(x=360,y=100)


window.mainloop()

Creating a button for our window.

For creating a button we will our Button tag.
Inside the button tag we will give the following arguments:- 
1. text(For the text shown in that button)
2. font(For adding a font to the text inside that button)
3. command(In this argument we have to give the name of the function which starts running when we click the button)

from tkinter import *

window = Tk()
window.minsize(600,400)
window.maxsize(600,400)
window.title('Calculator')

def main_function():
  pass

heading = Label(window,text='Calculator',font=('bold',50),bg='cyan')
heading.pack(fill=X)

num_1 = Entry(window,bg='light cyan',font=('normal',30),width=5)
num_1.place(x=140,y=100)

operation = Entry(window,bg='light cyan',font=('normal',30),width=1)
operation.place(x=290,y=100)

num_2 = Entry(window,bg='light cyan',font=('normal',30),width=5)
num_2.place(x=360,y=100)

button = Button(window,text='Enter',font=('bold',30),command=lambda:main_function())
button.place(x=270,y=200)

window.mainloop()
This is how our button will look like

Creating a message box for display of the results.

For creating a message box we will use message tag. We can not see the message box until we apply bg color to it or we insert any text inside it.
Inside the message tag we will give the following arguments:- 

  1. text(For adding text inside the message box)
  2. font(For adding a font to the text inside that message box)
  3. width(For giving a fixed width to the message box)
from tkinter import *

window = Tk()
window.minsize(600,400)
window.maxsize(600,400)
window.title('Calculator')

def main_function():
    pass

heading = Label(window,text='Calculator',font=('bold',50),bg='cyan')
heading.pack(fill=X)

num_1 = Entry(window,bg='light cyan',font=('normal',30),width=5)
num_1.place(x=140,y=100)

operation = Entry(window,bg='light cyan',font=('normal',30),width=1)
operation.place(x=290,y=100)

num_2 = Entry(window,bg='light cyan',font=('normal',30),width=5)
num_2.place(x=360,y=100)

button = Button(window,text='Enter',font=('bold',30),command=lambda:main_function())
button.place(x=270,y=200)

answer = Message(window,text=' ',font=('bold',30),width=600)
answer.place(x=100,y=300)


window.mainloop()

Writing code inside the main function.

We will use if / else statements for this program. We will take three inputs and convert them into integer value. In the operational value we will see that whether its – ‘-‘ , ‘+’ , ‘*’ , ‘/’ and will solve the two numbers accordingly.

from tkinter import *

window = Tk()
window.minsize(600,400)
window.maxsize(600,400)
window.title('Calculator')


# This is a main function of this program and all the code 
def main_function():
    op= str(operation.get())
    number_1 = int(num_1.get())
    number_2 = int(num_2.get())

    if op=='+':
        answer.config(text=f'{number_1} {op} {number_2} = {number_1+number_2}')

    elif op=='-':
        answer.config(text=f'{number_1} {op} {number_2} = {number_1 - number_2}')

    elif op=='*':
        answer.config(text=f'{number_1} {op} {number_2} = {number_1 * number_2}')

    elif op=='/':
        answer.config(text=f'{number_1} {op} {number_2} = {number_1 / number_2}')

    else:
        answer.config(text=f'Please enter a valid number')



heading = Label(window,text='Calculator',font=('bold',50),bg='cyan')
heading.pack(fill=X)

num_1 = Entry(window,bg='light cyan',font=('normal',30),width=5)
num_1.place(x=140,y=100)

operation = Entry(window,bg='light cyan',font=('normal',30),width=1)
operation.place(x=290,y=100)

num_2 = Entry(window,bg='light cyan',font=('normal',30),width=5)
num_2.place(x=360,y=100)

button = Button(window,text='Enter',font=('bold',30),command=lambda:main_function())
button.place(x=270,y=200)

answer = Message(window,text=' ',font=('bold',30),width=600)
answer.place(x=100,y=300)

window.mainloop()
GUI Calculator In Python

Leave a Reply

Your email address will not be published. Required fields are marked *