variable object with both scales. As we learned in the last section, changing a widget
changes its variable, but changing a variable also changes all the widgets it is associated
with. In the world of sliders, moving the slide updates that variable, which in turn might
update other widgets associated with the same variable. Because this script links one
variable with two scales, it keeps them automatically in sync: moving one scale moves
the other, too, because the shared variable is changed in the process and so updates the
other scale as a side effect.
Linking scales like this may or may not be typical of your applications (and borders on
deep magic), but it’s a powerful tool once you get your mind around it. By linking
multiple widgets on a display with tkinter variables, you can keep them automatically
in sync, without making manual adjustments in callback handlers. On the other hand,
the synchronization could be implemented without a shared variable at all by calling
one scale’s set method from a move callback handler of the other. I’ll leave such a
manual mutation as a suggested exercise, though. One person’s deep magic might be
another’s useful hack.
Running GUI Code Three Ways
Now that we’ve built a handful of similar demo launcher programs, let’s write a few
top-level scripts to combine them. Because the demos were coded as both reusable
classes and scripts, they can be deployed as attached frame components, run in their
own top-level windows, and launched as standalone programs. All three options illus-
trate code reuse in action.
Attaching Frames
To illustrate hierarchical GUI composition on a grander scale than we’ve seen so far,
Example 8-32 arranges to show all four of the dialog launcher bar scripts of this chapter
in a single container. It reuses Examples 8-9, 8-22, 8-25, and 8-30.
Example 8-32. PP4E\Gui\Tour\demoAll-frm.py
"""
4 demo class components (subframes) on one window;
there are 5 Quitter buttons on this one window too, and each kills entire gui;
GUIs can be reused as frames in container, independent windows, or processes;
"""
from tkinter import *
from quitter import Quitter
demoModules = ['demoDlg', 'demoCheck', 'demoRadio', 'demoScale']
parts = []
def addComponents(root):
for demo in demoModules:
module = import(demo) # import by name string
part = module.Demo(root) # attach an instance
Running GUI Code Three Ways | 471