A calculator is a machine that helps us solve simple mathematical problems instantly, such as addition, subtraction, etc. Ever found it hard to multiply 5 digit numbers. You would have probably used it many times but have you ever wondered how it is actually made? Now let us get right into it. Shall we?

SOURCE CODE:-

Now let us start with the program. So first we import the Tkinter module to access its functions. Then we define the variable root as Tk() which is done for creating an empty window used to define the GUI. Then we use the title function to create a title for my window/GUI lets keep it as “Calculator GUI” for now. Then we have to define a grid for the GUI which means we have to code how the output GUI should look for it to be like a calculator on your PC/ Phone. Hence we use the width function and assign it a value of 35 which is perfect for a calculator. And the border width can be kept at 5 which is standard. We use the grid function to define PADX (Used to put space between the widgets and the closest window) and PADY (Used to put space between borders of the frame and the root window)

from tkinter import*
root=Tk()
root.title("CALCULATOR GUI") 
e=Entry(root,width=35, borderwidth=5)
e.grid(row=0,column=0,columnspan=3,padx=10,pady=10)

Now we will define a function that does a particular command assigned to it after we click on it. Firstly for the numbers on the calculator. Now while typing in a calculator when we type all the numbers one by one we can see that the number appears on the box as a number according to the last number typed falls in the unit’s place and the other number comes in the other places. Hence for that, we have to add the current number clicked with the first number entered to the numbers already entered.

def button_click(number):
    current = e.get()    
    e.delete(0, END)    
    e.insert(0, str(current) + str(number))

In the calculator for the clear button, we use the delete function to delete the numbers from 0-which is the first number entered and till END-which the last number entered.

def button_clear():    
    e.delete(0, END)

Now for the button +, – *, and /, we need to define their functions after clicking on the buttons. Now for the addition button, we will define two global variables f_num- which will be the first number entered and we will define math depending on the button clicked by the user. If the user clicks on the ‘+’ math is assigned as ‘addition’ and so on. Now with the equal button what we do is evaluate the action clicked by the user using the get function.

def button_add(first_number):   
    first_number = e.get()    
    global f_num    
    global math    
    math = "addition"    
    f_num = int(first_number)    
    e.delete(0, END)      
def button_equal():    
    second_number = e.get()    
    e.delete(0, END)

Now as I wrote earlier as the user clicks the button ‘+’ the “addition” is assigned to the variable math. And hence we use the if statement to compare the math variable with all four buttons and display it in the top bar using e.insert

if math == "addition":         
   e.insert(0,f_num + int(second_number))             
if math == "subtraction":         
   e.insert(0,f_num - int(second_number))             
if math == "multiplication":         
   e.insert(0,f_num * int(second_number))             
if math == "division":         
   e.insert(0,f_num / int(second_number))

Now we define the button for subtracting two number

def button_subtract():
    first_number = e.get()
    global f_num
    global math
    math = "subtraction"
    f_num = int(first_number)
    e.delete(0, END)

We do the same thing for multiplications and divisions

def button_multiply():
    first_number = e.get()
    global f_num
    global math
    math = "multiplication"
    f_num = int(first_number)
    e.delete(0, END)
def button_divide():
    first_number = e.get()
    global f_num
    global math
    math = "division"
    f_num = int(first_number)
    e.delete(0, END)

Now we will describe the buttons. For each button, we will define a fixed grid ‘padx=40,pady=20’, so that all the buttons look equal. We assign the lambda function which is used to take any number of arguments but can only have one expression so that one button can be pressed again and again.

button_1 = Button(root,text="1",padx=40,pady=20,command=lambda: button_click(1))
button_2 = Button(root,text="2",padx=40,pady=20,command=lambda: button_click(2))
button_3 = Button(root,text="3",padx=40,pady=20,command=lambda: button_click(3))
button_4 = Button(root,text="4",padx=40,pady=20,command=lambda: button_click(4))
button_5 = Button(root,text="5",padx=40,pady=20,command=lambda: button_click(5))
button_6 = Button(root,text="6",padx=40,pady=20,command=lambda: button_click(6))
button_7 = Button(root,text="7",padx=40,pady=20,command=lambda: button_click(7))
button_8 = Button(root,text="8",padx=40,pady=20,command=lambda: button_click(8))
button_9 = Button(root,text="9",padx=40,pady=20,command=lambda: button_click(9))
button_0 = Button(root,text="0",padx=40,pady=20,command=lambda: button_click(0))
button_add = Button(root,text="+",padx=39,pady=20,command=button_add)
button_equal = Button(root,text="=",padx=100,pady=20,command=lambda: button_equal())
button_clear = Button(root,text="Clear",padx=90,pady=20,command = button_clear())
button_subtract = Button(root,text="-",padx=41,pady=20,command=button_subtract)
button_multiply = Button(root,text="*",padx=40,pady=20,command=button_multiply)
button_divide = Button(root,text="/",padx=40,pady=20,command=button_divide)

Now we place the buttons on the grid using the grid function which is used to plot the buttons. We refer to the digital calculator for their arrangement.

button_1.grid(row=3,column=0)
button_2.grid(row=3,column=1)
button_3.grid(row=3,column=2)
button_4.grid(row=2,column=0)
button_5.grid(row=2,column=1)
button_6.grid(row=2,column=2)
button_7.grid(row=1,column=0)
button_8.grid(row=1,column=1)
button_9.grid(row=1,column=2)

button_0.grid(row=4,column=0)
button_clear.grid(row=4,column=1,columnspan=2)
button_add.grid(row=5,column=0)
button_equal.grid(row=5,column=1,columnspan=2)

button_subtract.grid(row=6,column=0)
button_multiply.grid(row=6,column=1)
button_divide.grid(row=6,column=2)

Now for the output of the program and the display of the GUI, we use the ‘mainloop’ function.

root.mainloop()

The final code is:-

from tkinter import*

root=Tk()
root.title("CALCULATOR GUI") 

e=Entry(root,width=35, borderwidth=5)
e.grid(row=0,column=0,columnspan=3,padx=10,pady=10)      
 
#e.insert (0," ")

def button_click(number):
    current = e.get()
    e.delete(0, END)
    e.insert(0, str(current) + str(number))
    
def button_clear():
    e.delete(0, END)
    
def button_add(first_number):
    first_number = e.get()
    global f_num
    global math
    math = "addition"
    f_num = int(first_number)
    e.delete(0, END)
      
def button_equal():
    second_number = e.get()
    e.delete(0, END)
    
    if math == "addition":
         e.insert(0,f_num + int(second_number))
         
    if math == "subtraction":
         e.insert(0,f_num - int(second_number))
         
    if math == "multiplication":
         e.insert(0,f_num * int(second_number))
         
    if math == "division":
         e.insert(0,f_num / int(second_number))
        
def button_subtract():
    rfirst_number = e.get()
    global f_num
    global math
    math = "subtraction"
    f_num = int(first_number)
    e.delete(0, END)

def button_multiply():
    first_number = e.get()
    global f_num
    global math
    math = "multiplication"
    f_num = int(first_number)
    e.delete(0, END)

def button_divide():
    first_number = e.get()
    global f_num
    global math
    math = "division"
    f_num = int(first_number)
    e.delete(0, END) 
    
#Define buttons

button_1 = Button(root,text="1",padx=40,pady=20,command=lambda: button_click(1))
button_2 = Button(root,text="2",padx=40,pady=20,command=lambda: button_click(2))
button_3 = Button(root,text="3",padx=40,pady=20,command=lambda: button_click(3))
button_4 = Button(root,text="4",padx=40,pady=20,command=lambda: button_click(4))
button_5 = Button(root,text="5",padx=40,pady=20,command=lambda: button_click(5))
button_6 = Button(root,text="6",padx=40,pady=20,command=lambda: button_click(6))
button_7 = Button(root,text="7",padx=40,pady=20,command=lambda: button_click(7))
button_8 = Button(root,text="8",padx=40,pady=20,command=lambda: button_click(8))
button_9 = Button(root,text="9",padx=40,pady=20,command=lambda: button_click(9))
button_0 = Button(root,text="0",padx=40,pady=20,command=lambda: button_click(0))
button_add = Button(root,text="+",padx=39,pady=20,command=button_add)
button_equal = Button(root,text="=",padx=100,pady=20,command=lambda: button_equal())
button_clear = Button(root,text="Clear",padx=90,pady=20,command = button_clear())

button_subtract = Button(root,text="-",padx=41,pady=20,command=button_subtract)
button_multiply = Button(root,text="*",padx=40,pady=20,command=button_multiply)
button_divide = Button(root,text="/",padx=40,pady=20,command=button_divide)

#Put the buttons on the screen

button_1.grid(row=3,column=0)
button_2.grid(row=3,column=1)
button_3.grid(row=3,column=2)

button_4.grid(row=2,column=0)
button_5.grid(row=2,column=1)
button_6.grid(row=2,column=2)

button_7.grid(row=1,column=0)
button_8.grid(row=1,column=1)
button_9.grid(row=1,column=2)

button_0.grid(row=4,column=0)

button_clear.grid(row=4,column=1,columnspan=2)
button_add.grid(row=5,column=0)
button_equal.grid(row=5,column=1,columnspan=2)

button_subtract.grid(row=6,column=0)
button_multiply.grid(row=6,column=1)
button_divide.grid(row=6,column=2)

root.mainloop()    

OUTPUT:-

This is how the output looks like:-

Visits: 216

Leave a Reply

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