Hour 18. GUI Programming
What You’ll Learn in This Hour:
The basics of GUI programming
GUI Python libraries
Exploring the tkinter package
How to use GUI programming in Python
When you hear Python scripting, the first thing that probably comes to mind is boring command-line
scripts. This doesn’t have to be the case, though, if you plan on running your Python scripts in a
graphical environment—such as the Raspberry Pi. There are plenty of ways to interact with your
Python script other than the input and print statements! In this hour, you’ll learn how to add
graphical interfaces to your Python scripts to make them look more like Windows programs.
Programming for a GUI Environment
These days, just about every operating system incorporates some type of graphical user interface
(GUI) to allow users to input data and view results. This is true of Linux, which, you’ll remember, is
the operating system on the Raspberry Pi. While there are several different graphical desktop
environments in the Linux world, the Raspbian distribution used on the Raspberry Pi uses the LXDE
desktop package to provide a graphical desktop interface for users.
You can leverage the graphical desktop environment of the LXDE package with your Python scripts to
create a fancy window-oriented interface for your programs that will help give your scripts a more
professional look and feel.
Before we dive into the coding, though, it’s a good idea to first go through all the terms used in GUI
programming. If you’re brand new to the GUI programming world, there may be some things that
you’ve seen and used but never knew actually have names. The following sections walk through some
of the terminology and features of a GUI environment that you’ll need to become familiar with when
coding your Python scripts.
The Window Interface
When you make the move to GUI programming, you need to learn a new set of terms. For starters, the
main area in a window is called the frame. The frame contains all the objects the program uses to
interact with the user, and it is the central point in a GUI program.
The frame is composed of objects called widgets (short for window gadgets) that display and
retrieve information. Most graphical programming languages provide a library of widgets for you to
use in your programs. While not an official standard, there’s a common set of widgets available in
just about every graphical programming environment. Table 18.1 lists the widgets you’ll run into in
your Python GUI programming.