Calculator using PySimpleGUI - Python - GeeksforGeeks

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 ValueResult = ''  # Make Infinite Loopwhile 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 Modulefrom PySimpleGUI import *  # GUI Layoutlayout = [[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 PySimpleGUIform = FlexForm('CALCULATOR', default_button_element_size = (5, 2),                auto_size_buttons = False, grab_anywhere = False)form.Layout(layout)  # Result ValueResult = ''  # Make Infinite Loopwhile 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:
Python-gui
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Box Plot in Python using Matplotlib
Bar Plot in Matplotlib
Python | Get dictionary keys as a list
Python | Convert set into a list
Ways to filter Pandas DataFrame by column values
Python - Call function from another file
loops in python
Multithreading in Python | Set 2 (Synchronization)
Python Dictionary keys() method
Python Lambda Functions