I l@ve RuBoard Previous Section Next Section

6.1 "Here's Looking at You, Kid"

For most software systems, a graphical user interface (GUI) has become an expected part of the package. Even if the GUI acronym is new to you, chances are that you are already familiar with such interfaces -- the windows, buttons, and menus that we use to interact with software programs. In fact, most of what we do on computers today is done with some sort of point-and-click graphical interface. From web browsers to system tools, programs are routinely dressed-up with a GUI component to make them more flexible and easy to use.

In this part of the book, we will learn how to make Python scripts sprout such graphical interfaces too, by studying examples of programming with the Tkinter module -- a portable GUI library that is a standard part of the Python system. As we'll see, it's easy to program user interfaces in Python scripts, thanks both to the simplicity of the language and the power of its GUI libraries. As an added bonus, GUIs programmed in Python with Tkinter are automatically portable to all major computer systems.

6.1.1 GUI Programming Topics

Because GUIs are a major area, I want to say a few more words about this part of the book. To make them easier to absorb, GUI programming topics are split over the next four chapters of this book:

  • This chapter begins with a quick Tkinter tutorial to teach coding basics. Interfaces are kept simple here on purpose, so you can master the fundamentals before moving on to the following chapter's interfaces. On the other hand, this chapter covers all the basics: event processing, the pack geometry manager, using inheritance and composition in GUIs, and more. As we'll see, OOP (object-oriented programming) isn't required for Tkinter, but it makes GUIs structured and reusable.

  • Chapter 7 and Chapter 8 take you on a tour of the Tkinter widget set.[1] Roughly, Chapter 7, presents simple widgets, and Chapter 8, covers more advanced widgets and related tools. Most of the interfaces devices you're accustomed to seeing show up here: sliders, menus, dialogs, images, and their kin. These two chapters are not a fully complete Tkinter reference (which could easily fill a large book by itself), but should be enough to help you get started coding substantial Python GUIs. The examples in these chapters are focused on widgets and Tkinter tools, but Python's support for code reuse is also explored along the way.

    [1] The term "widget set" refers to the objects used to build familiar point-and-click user interface devices -- push-buttons, sliders, input fields, and so on. Tkinter comes with Python classes that correspond to all the widgets you're accustomed to seeing in graphical displays. Besides widgets, Tkinter also comes with tools for other activities, such as scheduling events to occur, waiting for socket data to arrive, and so on.

  • Chapter 9, presents more complex examples that use coding and widget techniques presented in the three preceding chapters. It begins with an exploration of techniques for automating common GUI tasks with Python. Although Tkinter is a full-featured library, a small amount of reusable Python code can make its interfaces even more powerful and easy to use. This chapter wraps up by presenting a handful of complete GUI programs that implement text editors, image viewers, clocks, and more.

Because GUIs are really cross-domain tools, other GUI examples will also show up throughout the remainder of this book. For example, we'll later see email GUIs, calculators, tree viewers, table browsers, and so on. See the fourth GUI chapter for a list of forward pointers to other Tkinter examples in this text.

One point I'd like to make right away: most GUIs are dynamic and interactive interfaces, and the best I can do here is show static screen shots representing selected states in the interactions such programs implement. This really won't do justice to most examples. If you are not working along with the examples already, I encourage you to run the GUI examples in this and later chapters on your own.

On Windows, the standard Python install comes with Tkinter support built-in, so all these examples should work immediately. For other systems, Pythons with Tkinter support are readily available as well (see Appendix B, and the top-level README-PP2E.txt file for more details). It's worth whatever extra install details you may need to absorb, though; experimenting with these programs is a great way to learn about both GUI programming, and Python itself.

Has Anyone Noticed That "GUI" Are the First Three Letters of "GUIDO"?

Python's creator didn't originally set out to build a GUI development tool, but Python's ease of use and rapid turnaround have made this one of its primary roles. From an implementation perspective, GUIs in Python are really just instances of C extensions, and extendability was one of the main ideas behind Python. When a script builds push-buttons and menus, it ultimately talks to a C library; and when a script responds to a user event, a C library ultimately talks back to Python.

But from a practical point of view, GUIs are a critical part of modern systems, and an ideal domain for a tool like Python. As we'll see, Python's simple syntax and object-oriented flavor blend well with the GUI model -- it's natural to represent each device drawn on a screen as a Python class. Moreover, Python's quick turnaround lets programmers experiment with alternative layouts and behavior rapidly, in ways not possible with traditional development techniques. In fact, you can usually make a change to a Python-based GUI, and observe its effects in a matter of seconds. Don't try this with C or C++.

    I l@ve RuBoard Previous Section Next Section