# Simple calculator using Python with Tkinter

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. 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)

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)

eql_button = Button(frame_button , fg = 'black',bg = 'white',text = '=',justify = CENTER,command = lambda: eql())

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_8 = Button(frame_number1 , fg = 'black',bg ='grey',text = '8',justify = CENTER,command = lambda : button_input(8))

n_9= Button(frame_number1 , fg = 'black',bg ='grey',text = '9',justify = CENTER,command =lambda :  button_input(9))

n_d= Button(frame_number1 , fg = 'black',bg ='white',text = '/',justify = CENTER,command =lambda :  button_input('/'))

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_5 = Button(frame_number2, fg = 'black',bg ='grey',text = '5',justify = CENTER,command = lambda : button_input(5))

n_6= Button(frame_number2 , fg = 'black',bg ='grey',text = '6',justify = CENTER,command = lambda : button_input(6))

n_m= Button(frame_number2 , fg = 'black',bg ='white',text = '*',justify = CENTER,command =lambda :  button_input('*'))

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_2 = Button(frame_number3, fg = 'black',bg ='grey',text = '2',justify = CENTER,command = lambda : button_input(2))

n_3= Button(frame_number3, fg = 'black',bg ='grey',text = '3',justify = CENTER,command = lambda : button_input(3))

n_s= Button(frame_number3, fg = 'black',bg ='white',text = '-',justify = CENTER,command = lambda : button_input('-'))

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_dot = Button(frame_number4, fg = 'black',bg ='grey',text = '.',justify = CENTER,command =lambda :  button_input('.'))

n_a= Button(frame_number4, fg = 'black',bg ='white',text = '+',justify = CENTER,command = lambda : button_input('+'))