Quick and easy with PySimpleGUI
Formatting Display Elements
The next step is to adjust the fonts, colors, and size properties of the graphic elements. With just three lines you can change the FORWARD button to 32 characters wide and three lines high with added color and a larger font:
[sg.Button("FORWARD", size=(32,3), font="Ariel 32", button_color=('white','green'))]
To make the rover control interface more usable, you can enlarge and add color to all the control buttons (Figure 5).
Raspberry Pi Rover
For my rover, I used a low-cost Arduino car chassis (~$15) and a portable phone charger to power the Raspberry Pi. Duct or painters tape works well to secure the Pi and charger to the car chassis.
Connecting motors directly to the Raspberry Pi GPIO pins is not recommended because their power requirements could damage your Raspberry Pi. If you're on a budget, you can create your own motor protection circuit with an L298N dual H-bridge chip (~$2); otherwise, you can find a variety of Pi motor or relay tops. For this project, I used a Pimoroni Explorer HAT Pro [2] (~$22).
For the final code (Listing 2), I added a command-line option (lines 6-10) that allows either a local Tkinter interface or a web interface. The program default is a Tkinter GUI; however, if any command-line text is entered, the PySimpleGUIWeb interface is used.
Listing 2
PySimpleGUI/Web Rover
01 # SGui_rover.py - use PySimpleGUI/Web to control a Pi Rover Pi 02 # 03 04 import sys 05 # Pass any command line argument for Web use 06 if len(sys.argv) > 1: # if there is use the Web Interface 07 import PySimpleGUIWeb as sg 08 mode = "web" 09 else: # default uses the tkinter GUI 10 import PySimpleGUI as sg 11 12 import RPi.GPIO as gpio 13 gpio.setmode(gpio.BOARD) 14 # Define the motor pins to match your setup 15 motor1pin = 38 # left motor 16 motor2pin = 37 # right motor 17 gpio.setup(motor1pin, gpio.OUT) 18 gpio.setup(motor2pin, gpio.OUT) 19 20 # Send Action to Control Rover 21 def rover(action): 22 if action == "FORWARD": 23 gpio.output(motor1pin, gpio.HIGH) 24 gpio.output(motor2pin, gpio.HIGH) 25 if action == "LEFT": 26 gpio.output(motor1pin, gpio.HIGH) 27 gpio.output(motor2pin, gpio.LOW) 28 if action == "RIGHT": 29 gpio.output(motor1pin, gpio.LOW) 30 gpio.output(motor2pin, gpio.HIGH) 31 if action == "STOP": 32 gpio.output(motor1pin, gpio.LOW) 33 gpio.output(motor2pin, gpio.LOW) 34 35 # All the stuff inside your window. 36 myfont = "Ariel 32" 37 layout = [ [sg.Text(" ",size=(20,1) , key="feedback")], 38 [sg.Button("FORWARD", size=(32,3), font=myfont, button_color=('white','green'))], 39 [sg.Button("LEFT", size=(15,3), font=myfont),sg.Button("RIGHT", size=(15,3), font=myfont)], 40 [sg.Button("STOP", size=(32,3), font=myfont, button_color=('white','red'))], 41 [sg.Button("QUIT")] 42 ] 43 # Create the Window 44 if mode == "web": 45 window = sg.Window('PySimpleGUI Rover Control', layout, 46 web_ip='192.168.0.106', web_port = 8888, web_start_browser=False) 47 else: 48 window = sg.Window('PySimpleGUI Rover Control', layout ) 49 50 # Event Loop to process "events" and pass them to the rover function 51 while True: 52 event, values = window.read() 53 print(event,values) 54 if event in (None, 'QUIT'): # if user closes window or clicks cancel 55 break 56 window['feedback'].Update(event) # show the button in the feedback text 57 rover(event) 58 59 window.close() # exit cleanly
The forward left and right motor pins are defined on lines 15 and 16. If you are using a Pi motor HAT or an L298N circuit, you can also define backward left and right motor pins. (Direct-wiring the Pi pins or using a relay top only supports one direction for the motors.)
A rover
function (lines 21-33) controls the motors according to button events. Figure 6 shows the Raspberry Pi rover with the web interface.
Summary
I was very happy with the performance and features offered by the PySimpleGUI library, and I found that the Pi rover project was a simple way to get started. For small Internet of Things (IoT) projects, you can use PySimpleGUI and PySimpleGUIWeb to create dashboard interfaces with bar and real-time charts.
Infos
- PySimpleGUI docs: https://pysimpleGUI.readthedocs.io/
- Pimoroni Explorer HAT Pro: https://www.adafruit.com/product/2427
« Previous 1 2
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
-
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.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.