In this tutorial we will try to create a
Simple Calculator Using Python. Python has a design philosophy which emphasizes code readability. That's why python is very easy to use especially for beginners who just started programming. It is very easy to learn the syntax emphasizes readability and it can reduces time consuming in developing. So let's now do the coding.
Setting up the Python
First you will need to download & install the Python IDLE's, here the link for the installer for python https://www.python.org/downloads/
After you installed the Pythons IDLE's, run the IDLE and then click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python.
Getting started
To create the calculator you need to import first a module called tkinter.
Tkinter is the standard toolkit in python for creating GUI(Graphical User Interface). Write the code that I provided below into the text editor.
Calculator Interface
This code will be the GUI of the calculator. Just simply copy the code then insert it into the text editor
def fCalc(src, side):
appObj = Frame(src, borderwidth=4, bd=2,bg = "#cccccc")
appObj.pack(side=side, expand=YES, fill=BOTH)
return appObj
def button(src, side, text, command=None):
appObj = Button(src, text=text, command=command)
appObj.pack(side=side, expand=YES, fill=BOTH)
return appObj
The Main Function Of The Calculator
This code will be the main program for the calculator, This is where the calculation is, and display the result of the given data. Just simply copy the code below.
class app(Frame):
def __init__(self, root = Tk(), width=364, height=425):
Frame.__init__(self)
self.option_add("*Font", 'arial 20 bold')
self.pack(expand=YES, fill=BOTH)
self.master.title("Simple Calculator")
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width/2) - (width/2)
y = (screen_height/2) - (height/2)
root.geometry('%dx%d+%d+%d' % (width, height, x, y))
root.resizable(0, 0)
display = StringVar()
Entry(self, relief= RIDGE,
textvariable=display, state=DISABLED, justify='right', bd=20, bg="silver").pack(side=TOP, expand=YES,
fill=BOTH)
clrChar = "Clear"
button(self, TOP, clrChar, lambda appObj=display, i=clrChar: appObj.set(''))
for btnNum in ("789/", "456*", "123-", "0.+"):
FunctionNum = fCalc(self, TOP)
for fEquals in btnNum:
button(FunctionNum, LEFT, fEquals,
lambda appObj=display, i=fEquals: appObj.set(appObj.get() + i))
EqualsButton = fCalc(self, TOP)
for fEquals in "=":
if fEquals == "=":
btnEquals = button(EqualsButton, LEFT, fEquals)
btnEquals.bind('<ButtonRelease-1>',
lambda e, s=self, appObj=display: s.result(appObj), "+")
else:
btnEquals = button(EqualsButton, LEFT, fEquals,
lambda appObj=display, s=" %s "%fEquals: appObj.set(appObj.get()+s))
def result(self, display):
try:
display.set(eval(display.get()))
except:
display.set("UNDEFINED")
Initializing the App
This code will initial the class called app, and display the Calculator Application.
if __name__ == '__main__':
app().mainloop()
After the code is completed you may now save it, then run to see if it works.
There you have we created a simple calculator using python programming. I hope that this tutorial help you improve in your programming carrier. For more updates and tutorials, just kindly visit this site. Enjoy Coding!!!