PySimpleGUI Entry Screen for Statistics Application

PySimpleGUI for Python Beginners

Example Code - 10 Aug 2019

by Paul F. Watson

Introduction: The PySimpleGUI libary enables easy and fast creation of Graphical User Interfaces (GUIs) for Python programmers. This critical capability facilitates rapid development of commercial and industrial software that meets user expectations.

Widgets for GUIs: This free Python GUI library provides great simplicity of use while offering an adequate selection of GUI Widgets including:

PySimpleGUI Versions: Like Python itself, PySimpleGUI is cross platform compatible, offering versions for Mac OS, Windows PC, Lenux PC and Raspberry Pi. PySimpleGUI 2.7 is compatible with Python 2.7 installations and PySimpleGUI is compatible with Python 3 installations. Whatever version you need may be downloaded for free from:

An internet search will quickly identify the needed download & installation steps.

Compatibility with IDE: PySimpleGUI sits 'atop' Ktinker and builds Ktinker widgets. My reading indicates that some programming approaches (IDEs and the like) use Ktinker for control of the IDE. Thus, the IDE itself may conflict with the code being written. I am currently using the CodeRunner IDE and have not experienced any problems.

Documentation: Good code examples and explanations are few for PySimpleGUI which was released in 2018. Many of the articles I did find contain information which is incompatible with current software releases. In response, I am offering a few examples to help beginning programmers "spin up quickly" with their GUI projects. For new users of PySimpleGUI, I would also like to suggest "readthedocs" which is part of github PySimpleGUI information. The presentation is a bit 'broad' in that it contains both very simple examples and overwhelming examples; but, there is much good information that can be pinched out.

Stability: PySimpleGUI seems to be a stable product; but, the Python error messages seem "less targeted" than they are for Python in general. Common typing errors such as leaving out commas between code lines for different widgets results in error messages which can "baffle" the careless programmer. PySimpleGUI seems a bit sensitive to unexpected spaces, tabs and the like. In my experience, if clean programming practices are followed, PySimpleGUI is a stable product.

PySimpleGUI Examples: The following examples were developed on a Mac with High Sierra OS using Python 2.7. I used the CodeRunner IDE and the examples worked fine. But some of the code lines are very long & it is possible you may need to use a 'continued line' to make them work on your system. If you find something that does not work on your system, feel free to send me an e-mail identifying which example didn't work & any error messages that resulted. Finally, two remaining points:

  1. There are small differences between the Python 2.7 and Python3 GUI code. One of those differences is the library import statement:
  2. When you write a real application with a GUI interface, you will very likely want to make the GUIs into functions & have the main program call them when needed. I have written the examples as stand-alone programs to keep things as simple as possible.

Why am I providing very simple examples? We are thankful to the many talented people who contribute their knowledge and examples on the internet. But often, the very tallented pack as much as possible into their examples. Inclusion of nonessential command elements districts from core elements of commands. The examples below are simplified in the belief that 'less is more'. In the belief that starting simple & building toward the complex is the natural evolution of things.

Reader, please read the comment lines at the top of each example as they provide useful information.

Example 1: Text Box Entry:

Example 1 requests the user's name. It also provides a 'submit' button. When the 'submit' button is pressed, a message is printed.
# Example- Text Entry Box with a Pop-up Greeting based on user input
# Very basic form to request text input.
# Returns values from GUI as a list in last line

import PySimpleGUI27 as sg

form = sg.FlexForm('Simple data entry form') 

layout =[sg.Text('What is your Name?', text_color='blue')],[sg.InputText()],[sg.Submit(), sg.Cancel()]

window=sg.Window('Cat Barn', layout)

button,values=window.Read()

print(button, values[0])

sg.Popup('Hello ' + values[0] + '. Welcome to Planet Earth!')

quit()

Example 2: Prompts with Text Boxes:

Program Example 2 is similar to the previous example except that 3 text entry boxes are provided. At the end of the program, all data entries are printed.

# PySimpleGUI27 : Using a FlexForm with multiple entry fields
# Prompting provided before multiple text entry boxes
# See printed results from form at last line of listing

import PySimpleGUI27 as sg
form = sg.FlexForm('PPP Simple data entry form')
# begin with a blank form
layout = [[sg.Text('Please enter your First Name, Last Name, E-Mail')]   
		[sg.Text('First Name', size=(15, 1)), sg.InputText('X')],
		[sg.Text('Last Name', size=(15, 1)), sg.InputText('Y')],
		[sg.Text('E-mail Address', size=(15, 1)), sg.InputText('Z')],
		[sg.Submit(), sg.Cancel()]

window=sg.Window('Earth Residents Contact Data', layout)

button, values = window.Read()

window.Close() # added to fix downstream problem

print(button, values[0], values[1], values[2])

quit()

Example 3: ListBox with multiple selection. Listing includes a Yes/No Button to initiate reading of GUI panel:

This example displays a list box and allows multiple inputs. Finally, a "Do It" button is provided to initiate computer reading of user inputs.

# PySimpleGUI27 Example: ListBox that accepts multiple inputs, before printing
# Create layout. Be sure and use Multiple Select mode option as shown
# if Multiple Select mode not chosen, it will only let you pick one item
# include OK button after all selections are made
# note that the layout command incorporated a second widget type - the OK button

import PySimpleGUI as sg
layout=[[sg.Listbox(values=['Apples','Bananas','Coconut','Dates','EatBerry','Figs','Grapefruit'],select_mode=sg.LISTBOX_SELECT_MODE_MULTIPLE, enable_events=False, visible=True, size=(30,6))],
	[sg.Text('What Planet Do You Come From?'), sg.InputText('P')],
	[sg.Button('OK')]] # enable events

window=sg.Window('Title of the Window', layout) # now, create the window

button,values=window.Read() # now read the window & put return values into button & values

window.Close()  # close the window AFTER the .Read statement

Example 4: Multiple Widgets in Same Window:

Example 4 is perhaps the most interesting. It demonstrates how to incorporate multiple widgets of different types into a single GUI panel. Finally, a 'Do It' button is implemented to initiate reading of the GUI panel input.

#!/usr/bin/python
# Multi Widget Window with PySimpleGUI27
# Example below presents Slider in horizontal orientation
# The GUI panel presents three radio buttons, slaved together
# finally, a button that triggers python to read the window & produce return values to the main program
# values are printed by the i loop near the end

import PySimpleGUI27 as sg
form=sg.FlexForm('colors',auto_size_text=True,font=('Helvetica',14))
layout=[[sg.Text('Colors',text_color='black')],
	[sg.Slider(range=(1,80), orientation='h', size=(15,20),background_color='#7BEA0C')],
	[sg.Radio('coffee',group_id=1,background_color='red')],
	[sg.Radio('tea',group_id=1, background_color='blue')],
	[sg.Radio('milk', group_id=1, background_color='cyan')],
	[sg.Radio('OJ', group_id=1, background_color='green')],
	[sg.SimpleButton('Do It!',size=(8,1))],
	]
window=sg.Window('Cat Barn', layout)

button,values=window.Read()

print 'value of button is:'
	print button
print 'values of various controls are:'

for i in range(0,5):
	print values[i]

quit()

PySimpleGUI Commands: Basic implementation of PySimpleGUI follows a definite pattern:

Typing the examples provided, verifying that they run and looking at the output should equip the reader with knowledge to produce similar GUI control panels. The reader must type them in (not cut and paste), because the listings above have been converted to html.

Conclusion: The documentation provided by GitHub and others is helpful and should be read; but, I have found instances where critical information was not provided and had to be dug out of examples. Also, some of my compatriots may find a brief coded example most helpful. The above examples have been provided in the hopes that they will speed folks along the road to creating GUI interfaces with ease.

Last, I find PySimpleGUI to be rather reasonable. As sleep becons and Python taunts me, I lie awake thinking about how to create an interface. Can I simple mix [sg.Text with sg.Something else] in a set of brackets and get side by side widgets? Should I put commas between? I try it the next day and find that it works! PySimpleGUI is a really simple and fast tool that can greatly improve your user interface. There are many Python libraries that can create a GUI interface, but none so easily or quickly as PySimpleGUI.



Contact the author paul-watson@sbcglobal.net by e-mail.
© 2019 All Rights Reserved
Paul F. Watson


Dionysus.biz Home Page