Here’s the stdout text you get after pressing Peek—the results of these classes’ state
methods:
x11 [1, 0, 1, 1] [0]
win [1, 0, 0, 1] [1]
The two classes in this module demonstrate how easy it is to wrap tkinter interfaces to
make them easier to use; they completely abstract away many of the tricky parts of
radio button and check button bars. For instance, you can forget about linked variable
details completely if you use such higher-level classes instead—simply make objects
with option lists and call their state methods later. If you follow this path to its logical
conclusion, you might just wind up with a higher-level widget library on the order of
the Pmw package mentioned in Chapter 7.
On the other hand, these classes are still not universally applicable; if you need to run
actions when these buttons are pressed, for instance, you’ll need to use other high-level
interfaces. Luckily, Python/tkinter already provides plenty. Later in this book, we’ll
again use the widget combination and reuse techniques introduced in this section to
construct larger GUIs like text editors, email clients and calculators. For now, this first
chapter in the widget tour is about to make one last stop—the photo shop.
Images
In tkinter, graphical images are displayed by creating independent PhotoImage or
BitmapImage objects, and then attaching those image objects to other widgets via
image attribute settings. Buttons, labels, canvases, text, and menus can display images
by associating prebuilt image objects in this way. To illustrate, Example 8-37 throws a
picture up on a button.
Example 8-37. PP4E\Gui\Tour\imgButton.py
gifdir = "../gifs/"
from tkinter import *
win = Tk()
igm = PhotoImage(file=gifdir + "ora-pp.gif")
Button(win, image=igm).pack()
win.mainloop()
I could try to come up with a simpler example, but it would be tough—all this script
does is make a tkinter PhotoImage object for a GIF file stored in another directory, and
associate it with a Button widget’s image option. The result is captured in Figure 8-37.
484 | Chapter 8: A tkinter Tour, Part 1