Control your Rasp Pi projects with Python’s Tkinter library
The Tinkerer
Use Tkinter to control your Rasp Pi projects from a smartphone or tablet.
If you are looking for an easy way to control your Raspberry Pi projects, consider Python's Tkinter library. In about 20 lines of Python code, you can create a graphic program to control a Rasp Pi rover project (Figure 1). This article shows how to create a Python Tkinter application to control the Rasp Pi's General Purpose Input Output (GPIO) pins. I will also show you how to set up a desktop shortcut that you can use to easily launch the application, and I will configure a Virtual Network Computing (VNC) server so that a smartphone, tablet, or PC can control the Rasp Pi remotely.
Python Tkinter Library
Tkinter [1] is bundled with the standard Rasp Pi Python installation. The base Tkinter library contains 17 different widgets that you can use for graphic applications. These widgets can be defined with customizable colors, sizes, text, and commands. There are three geometry managers (Pack, Grid, and Place) that can be used to arrange the widgets. For Rasp Pi hardware projects, I found the Grid geometry manager was a good fit. Listing 1 is a simple test program that presents two buttons in a grid and prints feedback when the buttons are pressed. (Note: Python 2.7 references the library as Tkinter
, whereas Python 3 uses tkinter
, lowercase). Figure 2 shows this test program with some button feedback.
Listing 1
tk_test.py
Tkinter Rasp Pi GPIO Motor Example
The Rasp Pi communicates with external hardware through its GPIO pins. These GPIO pins only support 5V connections, so relays or custom tops are required to connect to higher-powered devices. Very low-powered motors can be directly connected to Rasp Pi GPIO pins; however, it is highly recommended that you use a Rasp Pi motor top or some other protection hardware.
Different Rasp Pi tops will vary on the GPIO pins used for their motor connections. For our hardware setup, GPIO pins 19 and 21 were connected to the left and right motors. The GPIO pins are accessed in Python using the RPi.GPIO library. The RPi.GPIO library is preinstalled in the Raspbian image. Listing 2 shows the full code required to drive two motors. For this example, pins 19 and 21 are set as outputs using the GPIO.setup call. A motor function is created to turn each motor on or off. The Tkinter code was enhanced to make the buttons larger and add color. Also, the Python lambda function was used for passing the motor states. Figure 3 shows the Tkinter GPIO control program.
Listing 2
tk_gpio.py
Rasp Pi Shortcut
To make the project easier to access, you can place a quick launch icon or shortcut on the Rasp Pi desktop (Figure 4). To configure a shortcut, add a file to the user's $HOME/desktop
directory:
nano $HOME/desktop/tk_gpio.desktop
The name, icon, and execution command are defined inside this file (Listing 3).
Listing 3
tk_gpio.desktop
Depending on how you plan to run your program, a shortcut might be enough; however, if you would like to control the project remotely, VNC is a good option.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.