How To Make A Calculator in Python with Example

The Program For Calculator In Python is written in python programming language, In this article I will teach you on How To Make A Calculator in Python.

Program For Calculator In Python : Project Information

Project Name:Program For Calculator In Python
Language/s Used:Python (GUI) Based
Python version (Recommended):2.x or 3.x
Database:None
Type:Python App
Developer:IT SOURCECODE
Updates:0
Program For Calculator In Python – Project Information

Python has a design philosophy which emphasizes code readability. That’s why python is very smooth to use mainly for novices who just started programming. It is very smooth to research the syntax emphasizes clarity and it could reduces time consuming in developing.

About The Project Calculator in Python

A Calculator Python Program was design in Calculator In Python GUI or Graphical User Interface using Calculator In Python Tkinter, This article also includes the downloadable Calculator Python Code for free.

Calculator Project In Python With S...
Calculator Project In Python With Source Code 2020 | Howto Make Calculator

To start creating the Program For Calculator In Python, make sure that you have PyCharm IDE installed in your computer.

Recommendations

By the way if you are new to python programming and you don’t know what would be the the Python IDE to use, I have here a list of Best Python IDE for Windows, Linux, Mac OS that will suit for you.

I also have here How to Download and Install Latest Version of Python on Windows.

Steps on How To Make A Calculator in Python with Example

Time needed: 6 minutes.

How To Make A Calculator in Python with Example

  • Step 1: Create a project name.

    First open PyCharm IDE and then create a “project name” after creating a project name click the “create” button.
    Calculator in Python Create Project

  • Step 2: Create python file.

    Second after creating a project name, “right click” your project name and then click “new” after that click the “python file“.
    Calculator in Python Create Python File

  • Step 3: Name your python file.

    Third after creating a python file, Name your python file after that click “enter“.
    Calculator in Python file Name

  • Step 4: The actual code.

    Fourth is the actual coding on How To create a Simple Calculator Program In Python and you are free to copy the given code below and download the full source code below.

Calculator in Python Code Explanations

1. Import Module

The code given below is for importing module.

from tkinter import*

In this piece of code which is importing the tkinter module for the design of this project.

2. GUI

The code given below is for the design or GUI of this project.

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

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))
        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))

In this module which is the codes for the design of this project.

3. Display Result

The code given below is for displaying the result.

    def result(self, display):
        try:
            display.set(eval(display.get()))
        except:
            display.set("UNDEFINED")

In this module which displaying the result you’ve enter in the text field, with conditioning.

4. Complete Source Code

from tkinter import*

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

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))
        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")

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


Output

Calculator in Python Output
Calculator in Python Output

Downloadable Source Code

I have here the list of Best Python Project with Source code free to download for free, I hope this can help you a lot.

Summary

The Program For Calculator In Python is written in python programming language, Python has a design philosophy which emphasizes code readability.

That’s why python is very smooth to use mainly for novices who just started programming. It is very smooth to research the syntax emphasizes clarity and it could reduces time consuming in developing.

Project file contains the python script (simple_calculator.py). In this article can help the beginners in developing python programming language, and also can develop their skills in programming because the syntax and the logic in this python programming is easy to catch up and understand.

Leave a Comment