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:
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:
# 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()
#!/usr/bin/python # 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) button,values=window.Read() print(button, values[0],values[1] ) sg.Popup('Hello ' + values[0] + '. Welcome to Planet Earth!') quit()
# 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()
# 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
#!/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()
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
paul-watson@sbcglobal.net
by e-mail.
© 2020
All Rights Reserved
Paul F. Watson