Calculator using PySimpleGUI - Python
Last Updated :
24 Feb, 2021
Prerequisite: PySimpleGUI, eval
PySimpleGUI is a Python package that enables Python programmers of all levels to create GUIs. You specify your GUI window using a "layout" that contains widgets (they're called "Elements" in PySimpleGUI). In this article, we will learn, how to make a calculator using PySimpleGUI in Python.
Before starting we need to install this package:
pip install PySimpleGUI
Approach:
- Import the PySimpleGUI Module
- Create GUI layout and Window
- Add any number of widgets to the main window
- Apply the event Trigger on the widgets.
Below is what the GUI looks like:
Let’s create a GUI-based simple calculator using the Python PySimpleGUI module, which can perform basic arithmetic operation's addition, subtraction, multiplication, and division.
Let's Understand step by step implementation:-
Step1: Create Text Box, Buttons
For creating a text box, we will use Txt() method.
Syntax: Txt(Enter Text, *attr)
Here will use the read button using ReadFormButton() method.
Syntax: ReadFormButton(Enter Button Text, *attr)
Step 2: Create an infinite loop, read the button value and perform the operation.
Python3
# Result Value
Result = ''
# Make Infinite Loop
while True:
# Button Values
button, value = form.Read()
# Check Press Button Values
if button=='c':
Result = ''
form.FindElement('input').Update(Result)
elif button=='«':
Result = Result[:-1]
form.FindElement('input').Update(Result)
elif len(Result) == 16 :
pass
# Results
elif button=='=':
Answer = eval(Result)
Answer = str(round(float(Answer),3))
form.FindElement('input').Update(Answer)
Result = Answer
# close the window
elif button=='Quit' or button==None:
break
else:
Result += button
form.FindElement('input').Update(Result)
Below is the full implementation :
Python3
# Import Module
from PySimpleGUI import *
# GUI Layout
layout = [[Txt('' * 10)],
[Text('', size = (15, 1), font = ('Helvetica', 18),
text_color = 'black', key = 'input')],
[Txt('' * 10)],
[ReadFormButton('c'), ReadFormButton('«')],
[ReadFormButton('7'), ReadFormButton('8'), ReadFormButton('9'), ReadFormButton('/')],
[ReadFormButton('4'), ReadFormButton('5'), ReadFormButton('6'), ReadFormButton('*')],
[ReadFormButton('1'), ReadFormButton('2'), ReadFormButton('3'), ReadFormButton('-')],
[ReadFormButton('.'), ReadFormButton('0'), ReadFormButton('='), ReadFormButton('+')],
]
# Set PySimpleGUI
form = FlexForm('CALCULATOR', default_button_element_size = (5, 2),
auto_size_buttons = False, grab_anywhere = False)
form.Layout(layout)
# Result Value
Result = ''
# Make Infinite Loop
while True:
# Button Values
button, value = form.Read()
# Check Press Button Values
if button == 'c':
Result = ''
form.FindElement('input').Update(Result)
elif button=='«':
Result = Result[:-1]
form.FindElement('input').Update(Result)
elif len(Result) == 16 :
pass
# Results
elif button == '=':
Answer = eval(Result)
Answer = str(round(float(Answer),3))
form.FindElement('input').Update(Answer)
Result = Answer
# close the window
elif button == 'Quit' or button == None:
break
else:
Result += button
form.FindElement('input').Update(Result)
Output:
Similar Reads
1/4 Mile Calculator using PyQt5 in Python Prerequisite : Introduction to pyqt-5Â In this article, we will see how we can create a quarter(1/4) mile calculator using PyQt5. Quarter mile calculator is used to determine the terminal speed (trap speed) and quarter-mile elapsed time (ET) of a vehicle based on its weight and power. Below image sh
5 min read
Python | Simple GUI calculator using Tkinter Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter outputs the fastest and easiest way to create GUI app
3 min read
Loan Calculator using PyQt5 in Python In this article, we will see how we can create a loan calculator using PyQt5, below is an image that shows how is the loan calculator will look like :Â PyQt5 is cross-platform GUI toolkit, a set of python bindings for Qt v5. One can develop an interactive desktop application with so much ease becaus
5 min read
How to make calculator using kivy | Python Kivy is a platform-independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications. ???????? Kivy Tutorial - Learn Kivy with Examples. In this artic
3 min read
Ratio Calculator using PyQt5 In this article we will see how we can create a ratio calculator using PyQt5. Ratio is a contrast, which exists between two particular numbers, is defined as ratio. This ratio calculator is developed to compute this contrast and find out the relationship between these numbers. Below is how the calcu
5 min read