Raspberry Pi GUI Tutorial: Create your own GUI (Graphical User Interface) with TkInter and Python
Having a Raspberry Pi GUI on a touch screen makes controlling your Raspberry Pi so much easier. With Python and TkInter one can develop his own GUI very easily. In this video we are going to see how to achieve that.
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!
The hardware setup of today’s project is this. I am using a Raspberry Pi A+ but you can use any Raspberry Pi you want that has 40 GPIO pins. I am also using a 5 Inch touch display from Waveshare. I have prepared a detailed tutorial on that display few weeks ago, you can check it out. If you don’t have a touch display, the Graphical User Interface we are developing will work fine with a mouse as well.
WHERE TO BUY PARTS
1. Raspberry Pi A+: http://goo.gl/8E9JNM
2. 5” TFT Display: http://goo.gl/d3S2lr
3. Small Wi-Fi Dongle: http://goo.gl/pXxyJx
4. Small Breadboard: http://bit.ly/SmallBreadboard
5. LED: http://bit.ly/375LEDS
6. Wires: http://bit.ly/WiresArduino
7. Resistor: http://bit.ly/ResistorsB
I have connected the positive leg of the LED via a 100Ω resistor to GPIO pin 40 of the Raspberry Pi. The other leg of the resistor is connected to GPIO 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 TkInter library. The program file is located at the desktop so I run the following commands in order to run it.
sudo python gui.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 TkInter library. It is the most commonly used library for Python to develop Graphical Interfaces. It is already included in your Raspberry Pi distribution so you don’t have to download anything at all. As you can see the code of the project is simple and small. We only need 37 lines of code. At first I configure pin 40 to act as an output and I set it to LOW. Next I create our main Window. The next command creates a font for the text that we are going to use. After that there are two functions that I am going to explain later. With these two commands, we set the title of the main window that we created and the dimensions on the screen. With the next two commands we create the exit button and we declare that when it is pressed we want to execute the exitProgram function. With the same technique we create the LED button which when pressed executes the ledOn function. The ledOn function checks the state of PIN40 of the Raspberry Pi board. If it is HIGH it makes it LOW, else if it is LOW it makes it HIGH. The exitProgram function when executed it simply exits the program. As you can see the code is very simple and you can easily modify it in order to fit your needs. You can download the code in a link in the description of the video. You can control anything with it and having on screen controls make things so much easier!
SUBSCRIBE ON YOUTUBE
Never miss a video: Subscribe to educ8s.tv