Coders Packet

Simple calculator using Python with Tkinter

By Pragya Mukherjee

This tutorial will walk you through making a simple calculator using Python's one of the most used GUI frameworks Tkinter.

This project is all about making a simple eye-catching calculator using Python with Tkinter. The calculator will look something like this.

Calculator_python_Tkinter

Isn't that really interesting? So let's get started.

Before getting started with the coding section of this project, make sure you have the latest version of Python installed on your machine. If not you can download it here.

In order to complete this project, you can use any of your favorite python IDE. I will be using Pycharm.

After you are all set, run this command in your command prompt to install Tkinter:

pip install tk

Let's now import Tkinter. Then make a window object with some suitable title and measurement. Follow the code snippet below:

from tkinter import *
window = Tk()
window.geometry('300x400')
window.title("Simple Calculator")
window.resizable(0,0) #Non-resizable window

Let's now write a couple of codes to demonstrate some functions that are going to be considered when we click on any number, the number will be added to the input field(the green color area as in the image above), again when we press '=' button, the expression in the input field must be evaluated, and when we press 'C' i.e Clear button, the input field must be cleared.

expr = "" #expression in input field
text_expr = StringVar()

def button_input(input_):
    global expr
    expr=expr + str(input_)
    text_expr.set(expr)

def clr():
    global expr
    expr = ""
    text_expr.set("")

def eql():
    try:
        global expr
        evaluated = str(eval(expr))
        text_expr.set(evaluated)
        expr = ""
    except:
        text_expr.set("Error")

The codes are quite self-explanatory.

The button_input() accepts the characters from numbers and the arithmetic operators and adds them as a string to the input field. Similarly, the clr() clears the input field, and eql() evaluates the string and displays the result. But in this function, I used exception handling to display "Error" in case there is any arithmetic error in the expression(divide by zero error, etc).

Now we will create some frames and add the necessary widgets for the input field and the buttons of the calculator. Follow the codes below, and you are done with your first calculator !!

frame1 = Frame(window , width = 300, height = 40)
frame1.pack(side=TOP)

input_field = Entry(frame1, text =text_expr, font=('arial',27),fg = 'black',bg = "green", width = 300,justify = RIGHT)
input_field.grid(row = 0,column= 0)
input_field.pack(ipady = 20)

frame_button = Frame (window,width = 300,height = 360)
frame_button.pack()

clear_button = Button(frame_button,fg = 'black' ,bg = 'white',text = 'C',justify = CENTER,command = clr)
clear_button.pack(side=LEFT,fill = BOTH,ipadx=106,ipady = 20)

eql_button = Button(frame_button , fg = 'black',bg = 'white',text = '=',justify = CENTER,command = lambda: eql())
eql_button.pack(side=LEFT,fill = BOTH,ipadx=80,ipady = 20)

frame_number1 = Frame (window,width = 300,height = 360)
frame_number1.pack()

n_7 = Button(frame_number1,fg = 'black' ,bg ='grey',text = '7',justify = CENTER,command = lambda : button_input(7))
n_7.pack(side=LEFT,fill = BOTH,ipadx=30,ipady = 20)

n_8 = Button(frame_number1 , fg = 'black',bg ='grey',text = '8',justify = CENTER,command = lambda : button_input(8))
n_8.pack(side=LEFT,fill = BOTH,ipadx=30,ipady = 20)

n_9= Button(frame_number1 , fg = 'black',bg ='grey',text = '9',justify = CENTER,command =lambda :  button_input(9))
n_9.pack(side=LEFT,fill = BOTH,ipadx=30,ipady = 20)

n_d= Button(frame_number1 , fg = 'black',bg ='white',text = '/',justify = CENTER,command =lambda :  button_input('/'))
n_d.pack(side=LEFT,fill = BOTH,ipadx=30,ipady = 20)


frame_number2 = Frame (window,width = 300,height = 360)
frame_number2.pack()

n_4 = Button(frame_number2,fg = 'black' ,bg ='grey',text = '4',justify = CENTER,command =lambda :  button_input(4))
n_4.pack(side=LEFT,fill = BOTH,ipadx=30,ipady = 20)

n_5 = Button(frame_number2, fg = 'black',bg ='grey',text = '5',justify = CENTER,command = lambda : button_input(5))
n_5.pack(side=LEFT,fill = BOTH,ipadx=30,ipady = 20)

n_6= Button(frame_number2 , fg = 'black',bg ='grey',text = '6',justify = CENTER,command = lambda : button_input(6))
n_6.pack(side=LEFT,fill = BOTH,ipadx=30,ipady = 20)

n_m= Button(frame_number2 , fg = 'black',bg ='white',text = '*',justify = CENTER,command =lambda :  button_input('*'))
n_m.pack(side=LEFT,fill = BOTH,ipadx=30,ipady = 20)

frame_number3 = Frame (window,width = 300,height = 360)
frame_number3.pack()


n_1= Button(frame_number3,fg = 'black' ,bg ='grey',text = '1',justify = CENTER,command = lambda : button_input(1))
n_1.pack(side=LEFT,fill = BOTH,ipadx=30,ipady = 20)

n_2 = Button(frame_number3, fg = 'black',bg ='grey',text = '2',justify = CENTER,command = lambda : button_input(2))
n_2.pack(side=LEFT,fill = BOTH,ipadx=30,ipady = 20)

n_3= Button(frame_number3, fg = 'black',bg ='grey',text = '3',justify = CENTER,command = lambda : button_input(3))
n_3.pack(side=LEFT,fill = BOTH,ipadx=30,ipady = 20)

n_s= Button(frame_number3, fg = 'black',bg ='white',text = '-',justify = CENTER,command = lambda : button_input('-'))
n_s.pack(side=LEFT,fill = BOTH,ipadx=30,ipady = 20)

frame_number4 = Frame (window,width = 300,height = 360)
frame_number4.pack()


n_0= Button(frame_number4,fg = 'black' ,bg ='grey',text = '0',justify = CENTER,command = lambda : button_input(0))
n_0.pack(side=LEFT,fill = BOTH,ipadx=70,ipady = 20)

n_dot = Button(frame_number4, fg = 'black',bg ='grey',text = '.',justify = CENTER,command =lambda :  button_input('.'))
n_dot.pack(side=LEFT,fill = BOTH,ipadx=30,ipady = 20)

n_a= Button(frame_number4, fg = 'black',bg ='white',text = '+',justify = CENTER,command = lambda : button_input('+'))
n_a.pack(side=LEFT,fill = BOTH,ipadx=30,ipady = 20)




window.mainloop()

Don't forget to download the zipped file for the complete source code. Happy Learning...!!

Download project

Reviews Report

Submitted by Pragya Mukherjee (MPragya)

Download packets of source code on Coders Packet