[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

GuiMaker Classes


In return for conforming to GuiMaker protocols and templates, client subclasses get a
Frame that knows how to automatically build up its own menus and toolbars from
template data structures. If you read the preceding chapter’s menu examples, you
probably know that this is a big win in terms of reduced coding requirements. Gui
Maker is also clever enough to export interfaces for both menu styles that we met in
Chapter 9:
GuiMakerWindowMenu
Implements Tk 8.0-style top-level window menus, useful for menus associated
with standalone programs and pop ups.
GuiMakerFrameMenu
Implements alternative Frame/Menubutton-based menus, useful for menus on ob-
jects embedded as components of a larger GUI.
Both classes build toolbars, export the same protocols, and expect to find the same
template structures; they differ only in the way they process menu templates. In fact,
one is simply a subclass of the other with a specialized menu maker method—only top-
level menu processing differs between the two styles (a Menu with Menu cascades rather
than a Frame with Menubuttons).

GuiMaker Self-Test


Like GuiMixin, when we run Example 10-3 as a top-level program, we trigger the self-
test logic at the bottom of its file; Figure 10-2 shows the windows we get. Three windows
come up, representing each of the self-test code’s TestApp c l a s s e s. A l l t h r e e h a v e a m e n u
and toolbar with the options specified in the template data structures created in the
self-test code: File and Edit menu pull downs, plus a Quit toolbar button and a standard
Help menu button. In the screenshot, one window’s File menu has been torn off and
the Edit menu of another is being pulled down; the lower window was resized for effect.
GuiMaker can be mixed in with other superclasses, but it’s primarily intended to serve
the same extending and embedding roles as a tkinter Frame widget class (which makes
sense, given that it’s really just a customized Frame with extra construction protocols).
In fact, its self-test combines a GuiMaker frame with the prior section’s GuiMixin tools
package class.
Because of the superclass relationships coded, two of the three windows get their
help callback handler from GuiMixin; TestAppWindowMenuBasic gets GuiMaker’s instead.
Notice that the order in which these two classes are mixed can be important: because
both GuiMixin a n d Frame d e f i n e a quit m e t h o d , w e n e e d t o l i s t t h e c l a s s f r o m w h i c h w e
want to get it first in the mixed class’s header line due to the left-to-right search rule of
multiple inheritance. To select GuiMixin’s methods, it should usually be listed before a
superclass derived from real widgets.

608 | Chapter 10: GUI Coding Techniques

Do


wnload from Wow! eBook <www.wowebook.com>

Free download pdf