Create Calculator in Python [Tkinter Library]

Hello friends how are you, today in this blog i will teach you how you can create a Calculator in Python using Tkinter GUI Library. This blog will help you definitely if you want to create any kind of window application in Python. I am using PyCharm IDE and now i am going to describe everything step by step so just go through this post to get complete knowledge.

If you want to create a very simple and interesting projects in Python then you can use this code for your project and if you find any difficulty to understand then let me know by comment i will fix it. 

There are many way to create a calculator in Python but my i am trying to use very simple code to create it.

If you want to understand this program then we must have knowledge of the given Python Language topics, if you have no idea about these topics then first learn it from our blog.

  • Function
  • Loops
  • Tkinter Label
  • Tkinter Button
  • Tkinter Entry

Widgets used to create Calculator

1.TextBox or EditText(Entry)
2.Button

Complete code for Calculator

from tkinter import *

def Calculator(source, side):
    storeObj = Frame(source, borderwidth=4, bd=4, bg="gray")
    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 15 bold')
        self.pack(expand=YES, fill=BOTH)
        self.master.title('My Calculator')

        display = StringVar()
        #textbox code
        Entry(self, relief=RIDGE, textvariable=display,
              justify='right'
              , bd=20, bg="gray").pack(side=TOP,
                                              expand=YES, fill=BOTH)
        #clear textbox code
        for clearButton in (["C"]):
            erase = Calculator(self, TOP)
            for ichar in clearButton:
                button(erase, LEFT, ichar, lambda
                    storeObj=display, q=ichar: storeObj.set(''))
        #Numerical buttons code
        for numButton in ("789/", "456*", "123-", "0.+"):
            FunctionNum = Calculator(self, TOP)
            for iEquals in numButton:
                button(FunctionNum, LEFT, iEquals, lambda
                    storeObj=display, q=iEquals: storeObj
                       .set(storeObj.get() + q))
        #Equal button code
        EqualButton = Calculator(self, TOP)
        for iEquals in "=":
            if iEquals == '=':
                btniEquals = button(EqualButton, LEFT, iEquals)
                btniEquals.bind('<ButtonRelease-1>', lambda e, s=self,
                                                            storeObj=display: s.calculate(storeObj), '+')


            else:
                btniEquals = button(EqualButton, LEFT, iEquals,
                                    lambda storeObj=display, s=' %s ' % iEquals: storeObj.set
                                    (storeObj.get() + s))
    #calculate and display result
    def calculate(self, display):
        try:
            display.set(eval(display.get()))
        except:
            display.set("Error")


if __name__ == '__main__':
    app().mainloop()

This program will take an expression from user in a Textbox and after clicking on Equal button provide the result in same Textbox, for better experience see the below screenshot.
Output Screenshots
1.Calculator


2.Operation



3.Result



If you like this blog then subscribe my blog and share it with your friends.






Post a Comment

0 Comments