Q:

How do you make a simple calculator in Python?

0

How do you make a simple calculator in Python?

In this exercise, you will learn a simple calculator program in Python. By using this, you can develop a simple calculator which displays the different arithmetical operations, i.e., addition, subtraction, multiplication and division.

Tkinter is a standard cross-platform package for creating GUIs. It is also called Tk interface. It is an original GUI library for Tcl (Tool Command Language). Tkinter comes pre-installed with Python. Tkinter is rich in widgets, like radiobutton, checkbutton, menu, frame, canvas and much more. Here, we have used this for developing a simple calculator program.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Simple Calculator Program in Python

from tkinter import * 

def frame(root, side):
    window = Frame(root)
    window.pack(side=side, expand=YES, fill=BOTH)
    return window 

def button(root, side, text, command=None):
    window = Button(root, text=text, command=command, background = 'lightgray')
    window.pack(side=side, expand=YES, fill=BOTH)
    return window 

class SimpleCalculator(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.pack(expand=YES, fill=BOTH)
        self.master.title('Simple Calculator')
        self.master.iconname("calculator")
        self.master.geometry('300x250')
        display = StringVar()
        
        Entry(self, relief=SUNKEN,
            textvariable=display,  background = 'lightyellow').pack(side=TOP, expand=YES,
            fill=BOTH)
        
        for key in ("123", "456", "789", "-0."):
            keyFrame = frame(self, TOP)
            for char in key:
                button(keyFrame, LEFT, char,
                lambda w=display, s=' %s '%char: w.set(w.get()+s))
                
        x = frame(self, TOP)
        for char in "+-*/=":
            if char == '=':
                btn = button(x, LEFT, char)
                btn.bind('',
                    lambda e, s=self, w=display: s.calc(w), '+')
            else:
                btn = button(x, LEFT, char,
                    lambda w=display, c=char: w.set(w.get()+' '+c+' '))
        clearF = frame(self, BOTTOM)
        button(clearF, LEFT, 'Clr', lambda w=display: w.set(''))
        
    def calc(self, display):
        try:
            display.set(eval(display.get()))
        except ValueError:
            display.set("ERROR")
            
if __name__ == '__main__':
    SimpleCalculator().mainloop()

Sample output of the above program -

Simple Calculator Program

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Exercises, Practice Questions and Solutions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How do you find the prime factor of a number in Py... >>
<< How do you transpose a matrix using list comprehen...