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",

To make the rover control interface more usable, you can enlarge and add color to all the control buttons (Figure 5).

Figure 5: PySimpleGUI rover GUI.

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 #
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
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)
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)
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='', web_port = 8888, web_start_browser=False)
47 else:
48     window = sg.Window('PySimpleGUI Rover Control', layout )
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)
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.

Figure 6: Raspberry Pi rover with PySimpleGUIWeb.


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.


  1. PySimpleGUI docs: https://pysimpleGUI.readthedocs.io/
  2. Pimoroni Explorer HAT Pro: https://www.adafruit.com/product/2427

The Author

You can investigate more neat projects by Pete Metcalfe and his daughters at https://funprojects.blog.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus