Raspberry Pi GUI tutorial

Dear friends welcome back! This is Nick from educ8s.tv and today we are going to see how to develop a simple Graphical User Interface in order to control the GPIO pins of our Raspberry Pi. Without any further delay, let’s get started.

What we are going to build today

The project we are building today is very simple but very useful. As you can see I am using an application that I have developed to turn on or off the LED simply by touching an on-screen button. In this video, we are going to see how to develop an application like this, and as you are going to find out, it is extremely easy!

Parts Needed

If you don’t have a touch display, the Graphical User Interface we are developing will work fine with a mouse as well. I have connected the positive leg of the LED via a 100Ω resistor to pin 40 (GPIO pin 21) of the Raspberry Pi. The other leg of the resistor is connected to pin 39 which is GND. The pins of the Raspberry Pi use 3.3V logic levels unlike Arduino which uses 5V, have that in mind. Then I connect the screen and we boot Raspberry Pi up.

All we have to do is to run this simple Python program I have developed using the guizero library. The program file is located at the desktop so I run the following commands in order to run it.

cd Desktop
sudo python3 demo.py

That’s it, the Graphical User Interface has appeared and when I press the button the LED lights up, when I press it again it goes off. When I press the exit button, the application quits. Now that we know how to run the application let’s see the code of it.

In order to develop this Graphical User Interface, we are going to use the guizero library. This library is the easiest library I have found to develop simple graphical user interfaces. Since it is not included in the Raspberry Pi OS we have to download it. All we have to do is to open the terminal and type the following command:

sudo pip3 install guizero

After a few seconds, the library is installed and we are ready to use the library in our code.

As you can see the code of the project is simple and small. We only need 25 lines of code. 

At first, I import the libraries we are going to use in the code. Next, we declare that we are going to connect an LED to GPIO pin 21 of the Raspberry Pi. After that, there are two functions that I am going to explain later.

Next, I create our main app window. We set the window title to “First GUI” and the dimension of it to 800×600 pixels.

The next step is to create the two Pushbuttons. We first create the LED ON button with this command. We declare the function to execute when the button is pressed, the text of the button, the alignment on the screen, and lastly the width and the height of the button. Be careful here, the width and height value is not pixels but characters. The last step is to set the text size of the button. 

Following the same procedure, we create the Exit button. When the exit button is pressed the exitApp function is executed and as you can see it just exits the app.

The toggleLED function is executed when the ledButton is pressed. If the LED is ON, it turns it OFF and changes the Button text to “LED ON” else it turns the LED ON and changes the Button text to LED OFF. 

As you can see the code is very simple and you can easily modify it in order to fit your needs. You can control anything with it and having on-screen controls make things so much easier!