# Simple Graphical Calculator - Python

Posted by Suraj Singh on January 11, 2017 · 15 mins read

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 91011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071` `import Tkinter# Functions For Doing Functions Automatically & Easilydef iframe(parent): store=Tkinter.Frame(parent) store.pack(fill='both', expand="yes") return store# Calculation Enginedef 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 windowroot=Tkinter.Tk(className=" Calculator")# Here String Variabledisplayvar=Tkinter.StringVar()# Creating Displaydisplaywidget=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 Buttonsbutton_data=[ (['C']), (['CE']), ('7','8','9','(',')'), ('4','5','6','*','**'), ('1','2','3','/','%'), ('.','0','=','+','-')]# Creating Dialing Padkeypad=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 Padkeypad_1=iframe(root)# Parent Windows Mainlooproot.mainloop()`

import Tkinter Module

`import Tkinter`

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

`# Functions For Doing Functions Automatically &amp; Easilydef 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 windowroot=Tkinter.Tk(className=" Calculator")# Here String Variabledisplayvar=Tkinter.StringVar()# Parent Windows Mainlooproot.mainloop()`

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

`# Creating Displaydisplaywidget=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 Buttonsbutton_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 Enginedef 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 Padkeypad=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 Padkeypad_1=iframe(root)`

In The End, Assemble All Code And Run It.

`import Tkinter# Functions For Doing Functions Automatically &amp; Easilydef iframe(parent):    store=Tkinter.Frame(parent)    store.pack(fill='both', expand="yes")    return store# Calculation Enginedef 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 windowroot=Tkinter.Tk(className=" Calculator")# Here String Variabledisplayvar=Tkinter.StringVar()# Creating Displaydisplaywidget=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 Buttonsbutton_data=[    (['C']),    (['CE']),    ('7','8','9','(',')'),    ('4','5','6','*','**'),    ('1','2','3','/','%'),    ('.','0','=','+','-')]# Creating Dialing Padkeypad=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 Padkeypad_1=iframe(root)# Parent Windows Mainlooproot.mainloop()`

I hope, You all Guyx Enjoyed It.