PySimpleGUI Code Examples for Python Learners

Code Examples for Fast GUI Creation - 9 September 2019

by Paul F. Watson

Screenshot 3 column data entry ENTRY
PySimpleGUI based Failure Data Input GUI Display

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 27 is compatible with Python 2.7 installations and PySimpleGUI is compatible with Python 3 installations. Support for Python 2.7 is being phased out and we should all be updating to Python 3 with the corresponding PySimpleGUI library. But, whatever version you decide to download may be obtained free from:

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

Compatibility with IDE: PySimpleGUI sits 'atop' TKinker and builds TKinker widgets. My reading indicates that some programming approaches (IDEs and the like) use TKinker 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. GitHub does provide 170 On Line examples & extensive learning material. Below, I am provide a few of my own 'super simple' examples to help learners "spin up quickly" with their GUI projects.

PySimpleGUI Examples: The following examples were developed and tested on a Mac with High Sierra OS, Python 2.7 and PySimpleGUI27. If your IDE conflicts with PySimpleGUI, try CodeRunner, PyCharm or Komodo. If one of these examples does not work on your system, please e-mail identifying the example number & provide the error message that resulted. Finally, two remaining points:

  1. New installations should all use Python 3 as Python 2.7 is being phased out. There are small differences between the Python2.7 and Python3 GUI code. One of those differences is the library you must use and its corresponding import statement:
  2. When you write a real application with a GUI interface, you will likely want to make the GUI code into a function & have the main program call the function when needed. I have written the examples as stand-alone programs to keep things simple.

Screenshot of single text entry with submit button
Screenshot of Example 1 Textbox GUI

Example 1: Text Box Entry:

Example 1 requests the user's name. It also provides a 'submit' button. When the 'submit' button is pressed, the user filled in' text field is accepted and a greeting is printed in a Popup. So you will note that this short example actually produces two GUI panels.
# 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)


print(button, values[0])

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

Screenshot of side by side text entry GUI
Screenshot of Example 2 Side by Side Textboxes in GUI Panel

Example 2- Side by Side Widgets

Example 2 is similar to the Text Box entry shown above EXCEPT that two side by side text entry boxes are provided. Side by side widgets are accomplished by adding a comma, followed by the code for the 2nd widget. This is all on same line as shown.
# PySimpleGUI side by side widgets. Similar to Example 1

# side by side Text Entry Box with a Pop-up Greeting based on user input
# Shows how to create side by side widgets
# 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("Left Input Field. Type something.", text_color="blue"),sg.InputText("Right Input Field")],
	[sg.Submit(), sg.Cancel()],
window=sg.Window('Cat Barn', layout)


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

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


screenshot of vertical text entry boxes
Screenshot of Example 3 - Tripple Text Entry GUI

Example 3: Prompts with Text Boxes:

Program Example 3 is similar to example 1 except that 3 text entry boxes are provided. I have also added default text in the input box. At the end of this short program, all data entries obtained from the GUI are printed on your computer screen.

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


Example 4: Shows how to create a ListBox with multiple selection:

After the ListBox, The GUI panel also includes a Yes/No Button at the bottom to initiate reading of GUI panel. Just before 'window.Close(), you can include the statement 'print button, values' to see information transferred out of the GUI Panel. The information is usually transferred out as a Python list, using the name 'values[]'. You will control actions of your program by using if and elif statements together with values[]. A second variable 'button' is generally transferred out of GUI panels. Button values can also be used for program control, or they can be used just to force a reading of the GUI panel so the program can proceed. This discussion applies to almost all GUI panels created with PySimpleGUI.

# 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
# print button,values
window.Close()  # close the window AFTER the .Read statement

Example 5: Multiple Widgets in Same Window:

Example 5 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.
screenshot of several types of widgets on one GUI panel
Multiple Widgets in Same GUI Panel

# 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
	[sg.Slider(range=(1,80), orientation='h', size=(15,20),background_color='#7BEA0C')],
	[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)


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

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


Conclusion: 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! Look at Example 2 above to see how this story plays out in terms of code.

PySimpleGUI is a really fast and easy tool that can greatly improve your user interface. There are many Python libraries that can create a GUI interface, but none easier than PySimpleGUI.

P.S. I have been using the Python PySimpleGui library for over a year now. With the passage of time, I have not found any significant issues. This is a good, easy to use and stable product. I can not recommend it more highly.

Contact the author by e-mail.
© 2020 All Rights Reserved
Paul F. Watson Home Page