Quick and easy with PySimpleGUI
Simple Is Good
Use the same code for your Python GUI and web apps.
The PySimpleGUI project has two main goals: a simpler method for creating graphical user interfaces (GUIs), and common code for Tkinter, QT, Wx, and web graphics. Although I feel comfortable doing my own Python Tkinter and web interfaces, having common code for both local GUI and web apps could be extremely useful, especially for Raspberry Pi projects. In this article, I introduce PySimpleGUI and create both a local GUI and a web interface to control a Raspberry Pi rover, all in less than 60 lines of code.
Getting Started
The Python PySimpleGUI [1] project has a number of "ports" or versions. The main full-featured version is for Tkinter-based graphics. The versions for Qt, Wx, and web graphics are still in development, so some testing will be required if you are hoping for full code compatibility between the different libraries.
Although you probably won't encounter a lot of cases in which you would want to flip between Qt, Wx, and Tkinter graphic engines, the possibility exists. Figure 1 shows examples of the same code applied to the different graphic libraries.
To install the default version of PySimpleGUI for Tkinter enter:
pip install pysimpleGUI
PySimpleGUI has a wide range of graphic widgets or elements. Graphic presentations are built by creating a layout variable, and elements are placed in separate rows defined by enclosing square brackets. A row can have a single element or multiple elements. Figure 2 is a graphic GUI example with three rows.
A Button Interface
The rover project I create in this article uses a five-row layout (Listing 1; Figure 3). The first row contains feedback text, and rows 2-5 contain buttons.
Listing 1
PySimpleGUI Button
01 # Create a simple graphic interface 02 # 03 import PySimpleGUI as sg 04 05 layout = [ [sg.Text("the feedback" , key="feedback")], 06 [sg.Button("FORWARD")], 07 [sg.Button("LEFT"),sg.Button("RIGHT")], 08 [sg.Button("STOP")], 09 [sg.Button("QUIT")] 10 ] 11 # Create the Window 12 window = sg.Window('My First App', layout) 13 # Event Loop to process "events" 14 while True: 15 event, values = window.read() 16 window['feedback'].Update(event) # show the button in the feedback text 17 print(event,values) 18 if event in (None, 'QUIT'): 19 break 20 window.close()
The PySimpleGUI sg.window()
method displays a window with the title and a layout definition (line 12). The window.read()
method returns events and values that have changed (line 15). The feedback text element (line 5) is given the key name feedback
, which is used by the Update
method to indicate which button is pressed (line 16).
Standalone Web Apps
The PySimpleGUIWeb library is excellent for creating a lightweight standalone web interface. The real beauty in PySimpleGUIWeb is that no HTML, style sheets, Ajax, or JavaScript code is required. PySimpleGUIWeb is ideal for small, single-user web interfaces, and it supports features like a tabbed interface, tables, and graphics; however, it would not be a good fit if you need a multipage-multiuser web environment.
To install PySimpleGUIWeb enter:
pip install remi pip install pysimpleGUIweb
If I use my earlier button example, but this time use the PySimpleGUIWeb library, the code is identical, except for some added window options (Figure 4). If you are working locally on a Raspberry Pi, you can use the default sg.window
settings; however, if you want to work remotely, you need to define a web server address (web_ip
) and port (web_port
) and to disable the auto launching of the a web browser (web_start_browser = False
).
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Wine 10 Includes Plenty to Excite Users
With its latest release, Wine has the usual crop of bug fixes and improvements, along with some exciting new features.
-
Linux Kernel 6.13 Offers Improvements for AMD/Apple Users
The latest Linux kernel is now available, and it includes plenty of improvements, especially for those who use AMD or Apple-based systems.
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.