Related Articles

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

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:

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up