# Simple Calculator using python Gui﻿ |Create Calculator using tkinter

Simple Calculator using python Gui – Python Me Gui Calculator Banane ke liye tkinter Python ka use hota hai. Design Simple python gui calculator ko Step by Step Wise Dekhte Hai & Python Gui Calculator Ko Create Karte Hai.

Also Read :- Python Tkinter Learn A to Z

### Simple Calculator using python Gui

Step 1 . Sabse Pahle Ham tkinter ko import karte hai .

```import tkinter as tk
from tkinter import ttk```

Step 2. Global Variable Create Karte hai.

`exp = " "`

Step 3. Function Create Karte Hai Entry Box ( Display ) Me Number & Arithmetic Opreater ko show karne ke liye.

```def press(num):        #function name press
global exp         # global variable
exp=exp+str(num)    # str(num) convert string
equation.set(exp)    # show equation.set(exp) display expression```

#### Python Gui Calculator

Step 4 . Fir Function Create Karte Hai Equal ( = Button ) Karne ke liye or Equal Karke Display karne Ke Liye

```def equalpress():    # function name equalpress
try:
global exp       #global variable
total = str(eval(exp))    # total karne ke liye
equation.set(total)          # total num display karne ke liye
exp = " "                    # show expression
except:
equation.set('error')      # error show karne ke liye
exp = " "                  # show expression
```

Step 5. Function create karte hai display Data Clear (Clear Button ) Karne ke liye.

```def clear ():         # function name clear
global exp
exp = " "
equation.set(" ")
```

### design simple python gui calculator

Ab Tak Hamne Function ko create kiya Ab Ham main progromming karte hai hai.

Step 6. Main Function Se start Karte Hai

`if __name__ == "__main__":    # START PROGRAM`

Step 7. Calculator Gui Ka size or name , icon , background color .

```dk = tk.Tk()        # define dk
dk.title('Calculator')   # Title Name
dk.iconbitmap('Calculator.ico')   # Icon
dk.geometry('258x170')          # Gui Small Size
dk.maxsize(width=258,height=170)   # Gui Maximum Size

dk.configure(bg='blue')          # Gui backgound color
```

Step 8. Ab Ham Number & arithmetic operator Display karne ke liye Entry Box Create Karte hai.

```equation = tk.StringVar()
dis_entry = ttk.Entry(dk,width = 40, state = 'readonly', background ='red', textvariable = equation)
dis_entry.grid(row = 0 , columnspan = 10, ipadx = 6 , ipady = 8)
dis_entry.focus()
```

Step 9 . Ab Ham Button Create Karke Hai 1 to 9 & 0 , +,-,/,*,Clear Button ,Equal Button ya Enter Button

```btn7 = ttk.Button(dk, text = '7' , width = 5 ,  command = lambda : press(7) )
btn7.grid(row = 1 , column = 0 ,ipady = 4 , ipadx = 2)

btn8 = ttk.Button(dk, text = '8' , width = 5 ,  command = lambda : press(8) )
btn8.grid(row = 1 , column = 1 ,ipady = 4, ipadx = 2)

btn9 = ttk.Button(dk, text = '9' , width = 5 ,  command = lambda : press(9) )
btn9.grid(row = 1 , column = 2,ipady = 4, ipadx = 2)

btnmines = ttk.Button(dk, text = '-' , width = 8 ,  command = lambda : press('-') )
btnmines.grid(row = 1 , column = 3 , ipady = 3, ipadx = 2)

btnmul = ttk.Button(dk, text = '*' , width = 8 ,  command = lambda : press("*") )
btnmul.grid(row = 1 , column = 4 , ipady = 3, ipadx = 2)

btn4 = ttk.Button(dk, text = '4' , width = 5 ,  command = lambda : press(4) )
btn4.grid(row = 2 , column = 0 ,ipady = 4 , ipadx = 2)

btn5 = ttk.Button(dk, text = '5' , width = 5 ,  command = lambda : press(5) )
btn5.grid(row = 2 , column = 1 ,ipady = 4, ipadx = 2)

btn6 = ttk.Button(dk, text = '6' , width = 5 ,  command = lambda : press(6) )
btn6.grid(row = 2 , column = 2,ipady = 4, ipadx = 3)

btnplus = ttk.Button(dk, text = '+' , width = 5 ,  command = lambda : press("+") )
btnplus.grid(row = 2 , column = 3,ipady = 4, ipadx = 10)

btndiv = ttk.Button(dk, text = '/' , width = 5 ,  command = lambda : press("/")  )
btndiv.grid(row = 2 , column = 4,ipady = 4, ipadx = 10)

btnequal = ttk.Button(dk, text = 'Enter' , width = 5,  command = equalpress )
btnequal.grid(row = 3 , column = 4,ipady = 4, ipadx = 10)

btn0= ttk.Button(dk, text = '0' , width = 5,  command = lambda : press(0)  )
btn0.grid(row = 3 , column = 3,ipady = 4, ipadx = 10)

btn3 = ttk.Button(dk, text = '3' , width = 5,  command = lambda : press(3)  )
btn3.grid(row = 3 , column = 0 ,ipady = 4 , ipadx = 2)

btn2 = ttk.Button(dk, text = '2' , width = 5,  command = lambda : press(2)  )
btn2.grid(row = 3 , column = 1 ,ipady = 4, ipadx = 2)

btn1 = ttk.Button(dk, text = '1' , width = 5 ,  command = lambda : press(1) )
btn1.grid(row = 3 , column = 2,ipady = 4, ipadx = 2)

btnclr = ttk.Button(dk, text = 'Clear' , width = 5 ,  command = clear )
btnclr.grid(row = 4 , columnspan = 6,ipady = 4, ipadx = 108)

```

Step 10. Ab Ham Total Coding ko ek sath ek column ek add karte hai or fir aap output dekh sakte hai .

```import tkinter as tk
from tkinter import ttk

exp = " "
def press(num):
global exp
exp+=str(num)
equation.set(exp)

def equalpress():
try:
global exp
total = str(eval(exp))
equation.set(total)
exp = " "
except:
equation.set('error')
exp = " "

def clear ():
global exp
exp = " "
equation.set(" ")

if __name__ == "__main__":

dk = tk.Tk()
dk.title('Calculator')
dk.geometry('258x170')
dk.maxsize(width=258,height=170)

dk.configure(bg='blue')

# imp

equation = tk.StringVar()
dis_entry = ttk.Entry(dk,width = 40, state = 'readonly', background ='red', textvariable = equation)
dis_entry.grid(row = 0 , columnspan = 10, ipadx = 6 , ipady = 8)
dis_entry.focus()

btn7 = ttk.Button(dk, text = '7' , width = 5 ,  command = lambda : press(7) )
btn7.grid(row = 1 , column = 0 ,ipady = 4 , ipadx = 2)

btn8 = ttk.Button(dk, text = '8' , width = 5 ,  command = lambda : press(8) )
btn8.grid(row = 1 , column = 1 ,ipady = 4, ipadx = 2)

btn9 = ttk.Button(dk, text = '9' , width = 5 ,  command = lambda : press(9) )
btn9.grid(row = 1 , column = 2,ipady = 4, ipadx = 2)

btnmines = ttk.Button(dk, text = '-' , width = 8 ,  command = lambda : press('-') )
btnmines.grid(row = 1 , column = 3 , ipady = 3, ipadx = 2)

btnmul = ttk.Button(dk, text = '*' , width = 8 ,  command = lambda : press("*") )
btnmul.grid(row = 1 , column = 4 , ipady = 3, ipadx = 2)

btn4 = ttk.Button(dk, text = '4' , width = 5 ,  command = lambda : press(4) )
btn4.grid(row = 2 , column = 0 ,ipady = 4 , ipadx = 2)

btn5 = ttk.Button(dk, text = '5' , width = 5 ,  command = lambda : press(5) )
btn5.grid(row = 2 , column = 1 ,ipady = 4, ipadx = 2)

btn6 = ttk.Button(dk, text = '6' , width = 5 ,  command = lambda : press(6) )
btn6.grid(row = 2 , column = 2,ipady = 4, ipadx = 3)

btnplus = ttk.Button(dk, text = '+' , width = 5 ,  command = lambda : press("+") )
btnplus.grid(row = 2 , column = 3,ipady = 4, ipadx = 10)

btndiv = ttk.Button(dk, text = '/' , width = 5 ,  command = lambda : press("/")  )
btndiv.grid(row = 2 , column = 4,ipady = 4, ipadx = 10)

btnequal = ttk.Button(dk, text = 'Enter' , width = 5,  command = equalpress )
btnequal.grid(row = 3 , column = 4,ipady = 4, ipadx = 10)

btn0= ttk.Button(dk, text = '0' , width = 5,  command = lambda : press(0)  )
btn0.grid(row = 3 , column = 3,ipady = 4, ipadx = 10)

btn3 = ttk.Button(dk, text = '3' , width = 5,  command = lambda : press(3)  )
btn3.grid(row = 3 , column = 0 ,ipady = 4 , ipadx = 2)

btn2 = ttk.Button(dk, text = '2' , width = 5,  command = lambda : press(2)  )
btn2.grid(row = 3 , column = 1 ,ipady = 4, ipadx = 2)

btn1 = ttk.Button(dk, text = '1' , width = 5 ,  command = lambda : press(1) )
btn1.grid(row = 3 , column = 2,ipady = 4, ipadx = 2)

btnclr = ttk.Button(dk, text = 'Clear' , width = 5 ,  command = clear )
btnclr.grid(row = 4 , columnspan = 6,ipady = 4, ipadx = 108)

dk.mainloop()```