Ainfographie.com logo animé

Raspberry Pi Tutorial: Create your own GUI (Graphical User Interface) with TkInter and Python

  1. Accueil
  2. Blog
  3. Tutoriels infographie
  4. Raspberry Pi Tutorial: Create your own GUI (Graphical User Interface) with TkInter and Python

Having a Graphical User Interface (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.

???? My Android Game: http://bit.ly/QuizOfKnowledge

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 a 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.

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 on the desktop so I run the following commands in order to run it.

cd Desktop

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.

——————–
Code & Parts
——————–

http://educ8s.tv/raspberry-pi-gui-tutorial

Want to learn to code?
????‍???? Check my new YouTube channel: https://bit.ly/3tku2n0

Vous devez vous connecter pour publier un commentaire.
Menu