Using Tkinter to Create a GUI (Graphical User Interface) on the Raspberry Pi

Ron Mourant
4 min readOct 3, 2016

--

Having GUIs for programs on the Raspberry Pi is a good idea. Mac and Windows applications have been doing this for many years. Tkinter can be used to easily create GUIs for Raspberry Pi applications.

Below is a simple application that has two tkinter buttons. One button toggles an LED on and off. The other button exits the application.

The window shown below appears on the Raspberry Pi screen when the application starts.

When a user clicks on the “Turn LED On” button, the LED is turned on and the button’s label changes as shown below.

Python 3.54 was used to create the “Using tkinter” window. The tkinter 8.6 module is embedded in Python versions 3.54 and greater. The Raspbian Jessie image, available from https://www.raspberrypi.org/downloads/raspbian/ contains Python 3.54. To see what versions of Python and tkinter you have: click on the Menu tab, choose Programing, and launch the Python 3 (IDLE) application. The window below appears:

This shows we are running Python 3.4.2
I have typed in: import tkinter, and on the next line, tkinter._test()

This brings up a tkinter window that shows tkinter’s version.

The Tkinter module that comes with Python 2 is an older version. In the 8.6 version, tkinter is imported and not Tkinter, and some modules have been renamed. For details see: https://pymotw.com/3/porting_notes.html

Our simple example application also includes the use of gpiozero 1.3.1. Gpiozero simplifies programming of GPIO devices and their associated circuits on Raspberry Pi computers. Documentation and examples can be found at: https://gpiozero.readthedocs.io/en/v1.3.1/ Gpiozero 1.3.1 is included in the latest release of Raspbian Jessie https://www.raspberrypi.org/downloads/raspbian/

Below we show the LED circuit and the source code of our program, tkButtons.py

The LED Circuit

Parts needed for the circuit include an LED, 330 Ohm resistor, and connector wires. A low-cost kit containing these items and more is available from: https://thepihut.com/products/camjam-edukit

A diagram of our circuit is shown below.

The positive leg of the LED (the longer one with a bend at the top) is connected to pin GPIO 21 on the Raspberry Pi computer. The negative leg is connected to a 330 Ohm resistor. The resistor goes to pin 39 (Ground) on the Pi. The circuit is controlled by the tkButtons.py program. The program makes GPIO 21 go high (causes current to flow which lights the LED) or low (current shuts off).

tkButtons.py

The source code of tkButtons.py is shown below. It shows how to use tkinter 8.6 with python 3.4.2. For a good introduction to using tkinter, a free sample chapter of the book, Tkinter GUI Application Development Blueprints, is available at: https://www.packtpub.com/mapt/book/Application%20Development/9781785889738

In line 1, tkinter is imported as tk. This enables the use of tk where the word tkinter would normally be required.

In line 2, the tkinter.font module is imported. This allows the designation of the myFont object in line 7.

The root tkinter object, win, is created in line 5.

Line 8 connects the positive leg of the LED on the breadboard to pin GPIO21 on the Raspberry Pi computer.

The ledToggle() function is in lines 10 thru 16. This function is called when a user clicks on the Turn LED On / Turn LED Off button. If the LED is on, it is turned off, and the button’s label is changed to Turn LED On. If the LED is off, it is turned on, and the button’s label is changed to Turn LED Off.

The exitProgram() function is in lines 18 and 19. It is called when a user clicks on the Exit button.

The tkinter ledButton object and the tkinter exitButton object are created in lines 21 and 23, respectively.

Lines 22 and 24 add the geometry of these buttons to the “Using tkinter” window using tkinter’s grid geometry manager.

tkinter works by continuously running a main loop which watches for events such as button presses. This loop is started in line 26.

References

More information about tkinter modules are given at:
http://www.tutorialspoint.com/python/python_gui_programming.htm

Many thanks to Nick at http://educ8s.tv for his video, Raspberry Pi Tutorial: Create your own GUI (Graphical User Interface) with TkInter and Python
https://www.youtube.com/watch?v=Bvq0LdBn0dY

Book by Bhaskar Chaudhary:

--

--

Ron Mourant
Ron Mourant

Written by Ron Mourant

TinyML, AI, Edge Impulse, Arduino, Raspberry Pi, Pickleball

Responses (2)