5/26/2017 Using Tkinter to Create a GUI (Graphical User Interface) on the Raspberry Pi
Ron Mourant Follow
Raspberry Pi, Python3, Python Bottle, tkinker, ESP8266, Trinket, Autodesk Fusion 360
Oct 3, 2016 4 min read
Using Tkinter to Create a GUI
(Graphical User Interface) on the
Raspberry Pi
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 o. The other button exits the
application.
The window shown below appears on the Raspberry Pi screen when
the application starts.
https://medium.com/@ronm333/using-tkinter-to-create-a-gui-graphical-user-interface-on-the-raspberry-pi-b382a6bf6497 1/9
5/26/2017 Using Tkinter to Create a GUI (Graphical User Interface) on the Raspberry Pi
When a user clicks on the Turn LED On button, the LED is turned on
and the buttons 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:
https://medium.com/@ronm333/using-tkinter-to-create-a-gui-graphical-user-interface-on-the-raspberry-pi-b382a6bf6497 2/9
5/26/2017 Using Tkinter to Create a GUI (Graphical User Interface) on the Raspberry Pi
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 tkinters 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 simplies 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/
https://medium.com/@ronm333/using-tkinter-to-create-a-gui-graphical-user-interface-on-the-raspberry-pi-b382a6bf6497 3/9
5/26/2017 Using Tkinter to Create a GUI (Graphical User Interface) on the Raspberry Pi
Below we show the LED circuit and the source code of our program,
tkButtons.py
The LEDCircuit
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 ow
which lights the LED) or low (current shuts o).
https://medium.com/@ronm333/using-tkinter-to-create-a-gui-graphical-user-interface-on-the-raspberry-pi-b382a6bf6497 4/9
5/26/2017 Using Tkinter to Create a GUI (Graphical User Interface) on the Raspberry Pi
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
https://medium.com/@ronm333/using-tkinter-to-create-a-gui-graphical-user-interface-on-the-raspberry-pi-b382a6bf6497 5/9
5/26/2017 Using Tkinter to Create a GUI (Graphical User Interface) on the Raspberry Pi
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 O button. If the
LED is on, it is turned o, and the buttons label is changed to Turn
LED On. If the LED is o, it is turned on, and the buttons label is
changed to Turn LED O.
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 tkinters 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.
https://medium.com/@ronm333/using-tkinter-to-create-a-gui-graphical-user-interface-on-the-raspberry-pi-b382a6bf6497 6/9
5/26/2017 Using Tkinter to Create a GUI (Graphical User Interface) on the Raspberry Pi
References
More information about tkinter modules are given at:
http://www.tutorialspoint.com/python/python_gui_programming.ht
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:
Tkinter GUI Application Development Blueprints
| PACKT Books
Master GUI programming in Tkinter as you design,
implement, and deliver ten real-world applications from
start to
w w w .packtpub.com
https://medium.com/@ronm333/using-tkinter-to-create-a-gui-graphical-user-interface-on-the-raspberry-pi-b382a6bf6497 7/9
5/26/2017 Using Tkinter to Create a GUI (Graphical User Interface) on the Raspberry Pi
https://medium.com/@ronm333/using-tkinter-to-create-a-gui-graphical-user-interface-on-the-raspberry-pi-b382a6bf6497 8/9
5/26/2017 Using Tkinter to Create a GUI (Graphical User Interface) on the Raspberry Pi
https://medium.com/@ronm333/using-tkinter-to-create-a-gui-graphical-user-interface-on-the-raspberry-pi-b382a6bf6497 9/9