There are a few details worth highlighting here:
- The quit method serves some of the same purpose as the reusable Quitter button
we used in earlier chapters. Because mixin classes can define a large library of
reusable methods, they can be a more powerful way to package reusable compo-
nents than individual classes. If the mixin is packaged well, we can get a lot more
from it than a single button’s callback. - The clone method makes a new in-process copy, in a new top-level window, of the
most specific class that mixes in a GuiMixin (self.class is the class object that
the instance was created from). Assuming that the class requires no constructor
arguments other than a parent container, this opens a new independent copy of
the window (pass in any extra constructor arguments required). - The browser method opens the ScrolledText object we wrote in Chapter 9 in a new
window and fills it with the text of a file to be viewed. As noted in the preceding
chapter, there is also a ScrolledText widget in standard library module
tkinter.scrolledtext, but its interface differs, it does not load a file automatically,
and it is prone to becoming deprecated (though it hasn’t over many years). For
reference, its alternative code is included. - The spawn method launches a Python program command line as a new independent
process and waits for it to end or not (depending on the default False wait
argument—GUIs usually shouldn’t wait). This method is simple, though, because
we wrapped launching details in the launchmodes module presented at the end of
Chapter 5. GuiMixin both fosters and practices good code reuse habits.
The GuiMixin class is meant to be a library of reusable tool methods and is essentially
useless by itself. In fact, it must generally be mixed with a Frame-based class to be used:
quit assumes it’s mixed with a Frame, and clone assumes it’s mixed with a widget class.
To satisfy such constraints, this module’s self-test code at the bottom combines Gui
Mixin with a Frame widget.
Figure 10-1 shows the scene created by the module’s self-test after pressing “clone” and
“spawn” once each, and then “help” in one of the three copies. Because they are separate
processes, windows started with “spawn” keep running after other windows are closed
and do not impact other windows when closed themselves; a “clone” window is in-
process instead—it is closed with others, but its “X” destroys just itself. Make sure your
PYTHONPATH includes the PP4E directory’s container for the cross-directory pack-
age imports in this example and later examples which use it.
We’ll see GuiMixin show up again as a mixin in later examples; that’s the whole point
of code reuse, after all. Although functions are often useful, classes support inheritance
and access to instance state, and provide an extra organizational structure—features
that are especially useful given the coding requirements of GUIs. For instance, many
of GuiMixin’s methods could be replaced with simple functions, but clone and quit
could not. The next section carries these talents of mixin classes even further.
602 | Chapter 10: GUI Coding Techniques