PySimpleGUI is a simple Python library that allows you to create graphical user interfaces (GUI) easily and quickly. In this tutorial, I will show you how to use PySimpleGUI to control a Raspberry Pi from a Windows computer.
To get started, make sure you have Python installed on both your Raspberry Pi and Windows computer. You can install PySimpleGUI by running the following command in your terminal or command prompt:
pip install PySimpleGUI
Once PySimpleGUI is installed, we can start building our GUI. In this tutorial, we will create a GUI that allows us to turn on and off an LED connected to the GPIO pins of the Raspberry Pi.
First, connect an LED to one of the GPIO pins on your Raspberry Pi. In this example, we will use pin 18. Connect the anode (+) of the LED to pin 18 and the cathode (-) to a GND pin.
Next, create a Python script on your Raspberry Pi that will control the LED. Here is an example script:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
while True:
GPIO.output(18, GPIO.HIGH)
time.sleep(1)
GPIO.output(18, GPIO.LOW)
time.sleep(1)
Save this script as led_control.py
on your Raspberry Pi.
Now, we need to create a GUI on our Windows computer using PySimpleGUI that will send commands to the Raspberry Pi to turn the LED on and off. Here is an example script for the GUI:
import PySimpleGUI as sg
import socket
# Connect to the Raspberry Pi
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("raspberrypi.local", 8000))
layout = [
[sg.Button("Turn On"), sg.Button("Turn Off")]
]
window = sg.Window("LED Control", layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
if event == "Turn On":
client_socket.send(b"on")
elif event == "Turn Off":
client_socket.send(b"off")
window.close()
client_socket.close()
Save this script as led_gui.py
on your Windows computer.
Before running the scripts, make sure to modify the IP address in the client socket connection to match the IP address of your Raspberry Pi. You can find the IP address by running ifconfig
on your Raspberry Pi.
Now, run the led_control.py
script on your Raspberry Pi and the led_gui.py
script on your Windows computer. You should see a simple GUI with two buttons that allow you to turn the LED on and off.
That’s it! You have now successfully controlled a Raspberry Pi from a Windows computer using PySimpleGUI. This is just a simple example, but you can expand on this idea to create more complex GUIs to control other aspects of your Raspberry Pi. Happy coding!
There's a bug logged against RealTime buttons BTW, so be aware of that should you attempt to use them. If you think it's something you'll be using soon please say so that it can be bumped up in priority.