### Simple Graphical Calculator - Python

Hello Friends,

Welcome Again To My Blog. Today,  In this tutorials Post, I gonna to show you how we can simply create a simple calculator app using python and Tkinter language.

So, let's Quickly Start.
First, of all copy all below provide code into a script and run it. Actually, I just want to say, 'First Try It! Then We will more further'.

In This Script I tried my best to Create this script more easy for learning and reading..

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71``` ```import Tkinter # Functions For Doing Functions Automatically & Easily def iframe(parent): store=Tkinter.Frame(parent) store.pack(fill='both', expand="yes") return store # Calculation Engine def dialnum(text): if text=="C": if len(displayvar.get())!=0: value=displayvar.get() displayvar.set(value[:-1]) elif text=="CE": displayvar.set('') elif text=="=": k=displayvar.get() try: displayvar.set(eval(k)) except: displayvar.set('Error') else: displayvar.set(displayvar.get()+text) return def ibutton(parent,text): store=Tkinter.Button(parent, text=text,bg="powder blue",relief="raised", font=('arial 15 bold'),\ width=10,height=2) store.pack(side='left',fill='both', expand="yes") store['command']=lambda: dialnum(store['text']) return store # Here Creating Parent window root=Tkinter.Tk(className=" Calculator") # Here String Variable displayvar=Tkinter.StringVar() # Creating Display displaywidget=Tkinter.Entry(iframe(root), font=('arial 55 bold'),fg="blue",\ justify="right", relief="sunken",bd=15, textvariable=displayvar ) displaywidget.pack(fill='both', expand="yes", side="top") # Creating Buttons button_data=[ (['C']), (['CE']), ('7','8','9','(',')'), ('4','5','6','*','**'), ('1','2','3','/','%'), ('.','0','=','+','-')] # Creating Dialing Pad keypad=iframe(root) for i in button_data: print i storeframe=iframe(keypad) storeframe.pack(fill='both', expand="yes", side="top") for j in i: ibutton(storeframe,j) # Creating Operater Pad keypad_1=iframe(root) # Parent Windows Mainloop root.mainloop()```

import Tkinter Module

```import Tkinter
```

Now, Create A Function To Automate The process of Creating tkinter frame.

```# Functions For Doing Functions Automatically &amp; Easily
def iframe(parent):
store=Tkinter.Frame(parent)
store.pack(fill='both', expand="yes")
return store

```

Create Tkinter Window Object and Trigger Mainloop function.

```# Here Creating Parent window
root=Tkinter.Tk(className=" Calculator")

# Here String Variable
displayvar=Tkinter.StringVar()

# Parent Windows Mainloop
root.mainloop()
```

These lines to Create Frame And Create Tkinter Entry Widget Into that Frame.

```# Creating Display
displaywidget=Tkinter.Entry(iframe(root), font=('arial 55 bold'),fg="blue",\
justify="right", relief="sunken",bd=15, textvariable=displayvar )
displaywidget.pack(fill='both', expand="yes", side="top")

# Creating Buttons
button_data=[
(['C']),
(['CE']),
('7','8','9','(',')'),
('4','5','6','*','**'),
('1','2','3','/','%'),
('.','0','=','+','-')]

```

Function To Check Click Button Type And Other Operation To Perform various simple function routine.

```# Calculation Engine
def dialnum(text):
if text=="C":
if len(displayvar.get())!=0:
value=displayvar.get()
displayvar.set(value[:-1])

elif text=="CE":
displayvar.set('')

elif text=="=":
k=displayvar.get()
try:
displayvar.set(eval(k))
except:
displayvar.set('Error')
else:
displayvar.set(displayvar.get()+text)

return

def ibutton(parent,text):
store=Tkinter.Button(parent, text=text,bg="powder blue",relief="raised", font=('arial 15 bold'),\
width=10,height=2)
store.pack(side='left',fill='both', expand="yes")
store['command']=lambda: dialnum(store['text'])
return store

```

Function To Create Dial Pad Buttons Easily

```# Creating Dialing Pad
for i in button_data:
print i
storeframe.pack(fill='both', expand="yes", side="top")
for j in i:
ibutton(storeframe,j)

```

In The End, Assemble All Code And Run It.

```import Tkinter

# Functions For Doing Functions Automatically &amp; Easily
def iframe(parent):
store=Tkinter.Frame(parent)
store.pack(fill='both', expand="yes")
return store

# Calculation Engine
def dialnum(text):
if text=="C":
if len(displayvar.get())!=0:
value=displayvar.get()
displayvar.set(value[:-1])

elif text=="CE":
displayvar.set('')

elif text=="=":
k=displayvar.get()
try:
displayvar.set(eval(k))
except:
displayvar.set('Error')
else:
displayvar.set(displayvar.get()+text)

return

def ibutton(parent,text):
store=Tkinter.Button(parent, text=text,bg="powder blue",relief="raised", font=('arial 15 bold'),\
width=10,height=2)
store.pack(side='left',fill='both', expand="yes")
store['command']=lambda: dialnum(store['text'])
return store

# Here Creating Parent window
root=Tkinter.Tk(className=" Calculator")

# Here String Variable
displayvar=Tkinter.StringVar()

# Creating Display
displaywidget=Tkinter.Entry(iframe(root), font=('arial 55 bold'),fg="blue",\
justify="right", relief="sunken",bd=15, textvariable=displayvar )
displaywidget.pack(fill='both', expand="yes", side="top")

# Creating Buttons
button_data=[
(['C']),
(['CE']),
('7','8','9','(',')'),
('4','5','6','*','**'),
('1','2','3','/','%'),
('.','0','=','+','-')]

for i in button_data:
print i
storeframe.pack(fill='both', expand="yes", side="top")
for j in i:
ibutton(storeframe,j)