Write a Python program to create a simple calculator using Tkinter.

This is a program to create a calculator using the Tkinter module of python with appropriate GUI and colorful interface

What is a calculator?

A calculator is a software that evaluates the mathematical expression given in it and provides appropriate output.

What is Tkinter in Python?

It is a python library used to make GUI(Graphical User Interface ). It has a lot of classes, functions, methods, and attributes defined within there are many other libraries like Tkinter they are as follows:

  • Kivy
  • PyQt5
  • LIbavg
  • PySimpleGUI
  • Pyforms
  • Wax, etc

There are many applications in the market that using python’s Tkinter library

like: Instagram, Google, Spotify, Dropbox, Netflix, etc

Tkinter’s useful method and function

Tk() : used to create window interface

Title: used to giving title to the app or the given window

Button: used to given inputs on the app or window

Entry: a field where we can see the action of the button

main loop: this enables the whole code to run in a window

Algorithm of the calculator

● Importing Tkinter module creating its window interface and defining some functions that are going to be used in buttons to give commands
● Giving exceptional handling function to show an error when required
● Now create buttons and entry fields to use make it useful and runnable
● Creating the main loop with respect to the given window name.

Program to create a simple calculator using Tkinter in Python

'''import tkinter as it is a GUI module'''
from tkinter import *
import tkinter as tk

'''creating a vacant variable for further use'''
exp=""
'''this function is for creating expression'''
def click(no):
    '''this keyword global is use to point the varible we create outside the loop'''
    global exp
    ''' here we add the number as string with the globally vacant variable'''
    exp=exp + str(no)
    '''update the exp by using set method '''
    equ.set(exp)
''' this function is for the final result to be given out'''
def equal():
    try:
        global exp
        total=str(eval(exp))
        equ.set(total)
        exp=""
        '''this exception handling is for zero division error'''

    except:
        print("error")
        equ.set("error")
''' this function to clear the output field for new implementation'''
def clear():
    global exp
    exp =""
    equ.set("")
'''create a GUI window'''
win=tk.Tk()
'''giving title'''
win.title("CALCULATOR")
''' it is a variable class created for a instance for this class'''
equ=StringVar()
'''creating a entry field for the input space'''
e1=Entry(win,text="text here",width=40,textvariable=equ).grid(row=0,column=0,rowspan=2,columnspan=4,padx=40,pady=10)
'''creating button for the interface and packing them with respected function'''
a1=tk.Button(win,text="1",padx=20,pady=10,bg="black",fg="white",command= lambda :click("1")).grid(row=2,column=0)
a2=tk.Button(win,text="2",padx=20,pady=10,bg="black",fg="white",command= lambda :click("2")).grid(row=2,column=1)
a3=tk.Button(win,text="3",padx=20,pady=10,bg="black",fg="white",command= lambda :click("3")).grid(row=2,column=2)
a4=tk.Button(win,text="4",padx=20,pady=10,bg="black",fg="white",command= lambda :click("4")).grid(row=3,column=0)
a5=tk.Button(win,text="5",padx=20,pady=10,bg="black",fg="white",command= lambda :click("5")).grid(row=3,column=1)
a6=tk.Button(win,text="6",padx=20,pady=10,bg="black",fg="white",command= lambda :click("6")).grid(row=3,column=2)
a7=tk.Button(win,text="7",padx=20,pady=10,bg="black",fg="white",command= lambda :click("7")).grid(row=4,column=0)
a8=tk.Button(win,text="8",padx=20,pady=10,bg="black",fg="white",command= lambda :click("8")).grid(row=4,column=1)
a9=tk.Button(win,text="9",padx=20,pady=10,bg="black",fg="white",command= lambda :click("9")).grid(row=4,column=2)
a0=tk.Button(win,text="0",padx=20,pady=10,bg="black",fg="white",command= lambda :click("0")).grid(row=5,column=1)
b1=tk.Button(win,text="+",padx=20,pady=10,bg="blue",fg="white",command= lambda :click("+")).grid(row=2,column=3)
b2=tk.Button(win,text="-",padx=20,pady=10,bg="blue",fg="white",command= lambda :click("-")).grid(row=3,column=3)
b3=tk.Button(win,text="*",padx=20,pady=10,bg="blue",fg="white",command= lambda :click("*")).grid(row=4,column=3)
b4=tk.Button(win,text="/",padx=20,pady=10,bg="blue",fg="white",command= lambda :click("/")).grid(row=5,column=3)
b5=tk.Button(win,text="=",padx=20,pady=10,bg="blue",fg="white",command= equal).grid(row=5,column=2)
b6=tk.Button(win,text="clear",padx=10,pady=10,bg="blue",fg="white",command=clear).grid(row=5,column=0)
'''calling the mainloop to run the whole tkinter code'''
win.mainloop()

Output