Easy GUIs for Python Programs

I recently got more interested in python after some MOOC classes on AI. I hadn’t really used it much since my thesis work which allowed users to write Jython scripts to control robots in my robotics simulator.  Since then I did a little webscraping project here or there but nothing big.

There was an annoying set of issues I had with python, one of which was that I couldn’t slap together GUIs as fast as I’d like to. I admit, Java had me spoiled with the wysiwyg builders in netbeans and eclipse. I honestly would gravitate to processing if I needed an interface, but now I’ve come across a stupid-simple GUI library for python called PySimpleGUI.

Basically this is a wrapper around the tKinter library (though QT and other graphics libs are available as well).  Much of setting up a GUI is done in a few lines of code. There’s a large number of example programs and a good bit of documentation. It’s slightly annoying but also awesome that the entire doc are in one giant webpage so no matter what you want, simply ctrl+f to find it on that page. Their gihub page is very insightful as well with insightful gems of example code in the issues section.  My dude even has a youtube channel full of great tutorials and explanations. I recommend getting up to speed with it starting with this playlist.

It’s as easy as installing the library:

pip install pysimplegui

One of the key features of PySimpleGUI is its simplicity. It abstracts away many of the complexities of GUI programming, making it easy for even beginners to create visually appealing and functional GUI applications. PySimpleGUI also offers a wide range of customization options, allowing users to tailor the appearance and behavior of their GUI applications to meet their specific needs.

In addition to its simplicity and customization capabilities, PySimpleGUI is also highly portable. It is compatible with multiple operating systems, including Windows, Linux, and macOS, and can be used with both Python 2 and Python 3. This makes it an excellent choice for developing GUI applications that need to be compatible with a variety of platforms.

PySimpleGUI lays out windows in a very simple grid form that is controlled by lists.  Let’s write an example to see how this works:

Check out the code below:

This is a simple converter from inches to centimeters.  Looking at the “layout” variable you can see a list which has 3 elements.

TextOutput, TextInput, Button

This is the order the elements will appear on the main screen. If I wanted to add a second row, I could simply make this a 2D list, each row containing a new line of elements in the GUI window.  For example the following puts the “OK” button on the next line down:

layout = [ [sg.Text('Enter inches'), sg.InputText(key = 'inputValue') ] , [sg.Ok()] ]

When GUI items like buttons, etc. are activated by the user, a message is sent to the “window.read()” function. You can then run if-else statements to do different things based on which button was pressed.

Of course it can get into much more complicated territory, but since there are so many example programs doing pretty much anything you can think of in one way or another it is easy to see how this library can be applied nearly anything written in python. Examples include a “how do I” app that basically tells you how to do anything you type in the question box, games like Uno and Chess, Matplotlib for graphing, even integrating with AI programs and making desktop widgets.

I’ve written a couple apps with it now for personal and professional use and I’m hooked. Definitely give it a try!

You can use trinket.io/pygame or replit.com to edit basic programs in your browwser online, but to run this code on your own computer, I use vscode. Here’s a quick tutorial on how to get that installed and set up in 5 minutes).

Overall, PySimpleGUI is a powerful and easy-to-use Python library that enables developers to create professional-quality GUI applications with minimal effort. Its simplicity, customization options, and portability make it a great choice for a wide range of projects, from small utility programs to large-scale applications.