Write Calculator App In Advance Way - Python

Posted by Suraj Singh on January 11, 2017 · 14 mins read
hii readers,



                                Welcome Again Guyz, To My Blog. readers In Today's Tutorial I am Going To Write about how to Write a Simple Calculator App Using Python Language In Simplest And Minimum Python Codes..
And I think, The Core Concepts Of Today's Tutorials Is To make Calculator App More Easy To Write, Small But Efficient Codes Actually, To be an expert, we need to start from beginners level. so, I think this tutorial is very important for beginners and for newbies also.

So, Let's Quickly Start, 
For this tutorial, we will use python Tkinter Module For Calculator GUI Because Tkinter is very easy to use. 

First of All, Guyx Copy Below Code And Run it In Your System. Because To Understand Anything First You have to Run It. So, Quickly Run Below Codes.

 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
#!/usr/bin/python
# This Example is Written By
__author__='''

######################################################
By
######################################################

Suraj Singh


surajsinghbisht054@gmail.com
http://www.bitforestinfo.com/


######################################################
'''

from Tkinter import *

def icalc(source, side):
storeobj=Frame(source, borderwidth=4, bd=4, bg="powder blue")
storeobj.pack(side=side, expand=YES, fill=BOTH)
return storeobj

def button(source, side, text, command=None):
storeobj=Button(source, text=text, command=command)
storeobj.pack(side=side, expand=YES, fill=BOTH)
return storeobj

class app(Frame):
def __init__(self):

Frame.__init__(self)
self.option_add('*Font','arial 20 bold')
self.pack(expand=YES, fill=BOTH)
self.master.title('calculator')
display=StringVar()
def calcu():
data=display.get()
if '.' not in data:
data1=data.replace('+', '.0+')
data2=data1.replace('-', '.0-')
data3=data2.replace('*', '.0*')
data4=data3.replace('/', '.0/')
try:
display.set(eval(data4))
except:
display.set('No Input Value')
else:
display.set(eval(data))
Entry(self, relief=SUNKEN, textvariable=display, bd=10,justify='right',bg='powder blue', width=30).pack(side=TOP, expand=YES, fill=BOTH)
for clearbut in (['CE'],['C']):
erase=icalc(self, TOP)
for ichar in clearbut:
button(erase, LEFT, ichar, lambda storeobj=display, q=ichar: storeobj.set(''))
for numbut in ("789/","456*","123-","0.+"):
functionnum=icalc(self, TOP)
for char in numbut:
button(functionnum, LEFT, char, lambda storeobj=display, q=char: storeobj.set(storeobj.get()+q))
equalbutton=icalc(self, TOP)
btequal=button(equalbutton, LEFT, '=', calcu)
if __name__=='__main__':
app().mainloop()


Now, Let me Explain you All Above Codes One By One.


from Tkinter import *

Function To handle Frame Creation, and Packing In One Step


def icalc(source, side):
storeobj=Frame(source, borderwidth=4, bd=4, bg="powder blue")
storeobj.pack(side=side, expand=YES, fill=BOTH)
return storeobj


Like Above Same For Button Also,


def button(source, side, text, command=None):
storeobj=Button(source, text=text, command=command)
storeobj.pack(side=side, expand=YES, fill=BOTH)
return storeobj


Calculator Window Object Class


class app(Frame):
def __init__(self):

Frame.__init__(self)
self.option_add('*Font','arial 20 bold')
self.pack(expand=YES, fill=BOTH)
self.master.title('calculator')


Function To handle Input Data Of Entry Box, Convert it Into Float And Then, Evaluate it .


  def calcu():
data=display.get()
if '.' not in data:
data1=data.replace('+', '.0+')
data2=data1.replace('-', '.0-')
data3=data2.replace('*', '.0*')
data4=data3.replace('/', '.0/')
try:
display.set(eval(data4))
except:
display.set('No Input Value')
else:
display.set(eval(data))

To Make Button And Entry Widget
  Entry(self, relief=SUNKEN, textvariable=display, bd=10,justify='right',bg='powder blue', width=30).pack(side=TOP, expand=YES, fill=BOTH)
for clearbut in (['CE'],['C']):
erase=icalc(self, TOP)
for ichar in clearbut:
button(erase, LEFT, ichar, lambda storeobj=display, q=ichar: storeobj.set(''))
for numbut in ("789/","456*","123-","0.+"):
functionnum=icalc(self, TOP)
for char in numbut:
button(functionnum, LEFT, char, lambda storeobj=display, q=char: storeobj.set(storeobj.get()+q))
equalbutton=icalc(self, TOP)
btequal=button(equalbutton, LEFT, '=', calcu)



In the End, Assemble All It.


from Tkinter import *

def icalc(source, side):
storeobj=Frame(source, borderwidth=4, bd=4, bg="powder blue")
storeobj.pack(side=side, expand=YES, fill=BOTH)
return storeobj

def button(source, side, text, command=None):
storeobj=Button(source, text=text, command=command)
storeobj.pack(side=side, expand=YES, fill=BOTH)
return storeobj

class app(Frame):
def __init__(self):

Frame.__init__(self)
self.option_add('*Font','arial 20 bold')
self.pack(expand=YES, fill=BOTH)
self.master.title('calculator')
display=StringVar()
def calcu():
data=display.get()
if '.' not in data:
data1=data.replace('+', '.0+')
data2=data1.replace('-', '.0-')
data3=data2.replace('*', '.0*')
data4=data3.replace('/', '.0/')
try:
display.set(eval(data4))
except:
display.set('No Input Value')
else:
display.set(eval(data))
Entry(self, relief=SUNKEN, textvariable=display, bd=10,justify='right',bg='powder blue', width=30).pack(side=TOP, expand=YES, fill=BOTH)
for clearbut in (['CE'],['C']):
erase=icalc(self, TOP)
for ichar in clearbut:
button(erase, LEFT, ichar, lambda storeobj=display, q=ichar: storeobj.set(''))
for numbut in ("789/","456*","123-","0.+"):
functionnum=icalc(self, TOP)
for char in numbut:
button(functionnum, LEFT, char, lambda storeobj=display, q=char: storeobj.set(storeobj.get()+q))
equalbutton=icalc(self, TOP)
btequal=button(equalbutton, LEFT, '=', calcu)
if __name__=='__main__':
app().mainloop()

Have A nice Day!