GuiMaker: Automating Menus and Toolbars
The last section’s mixin class makes common tasks simpler, but it still doesn’t address
the complexity of linking up widgets such as menus and toolbars. Of course, if we had
access to a GUI layout tool that generates Python code, this would not be an issue, at
least for some of the more static interfaces we may require. We’d design our widgets
interactively, press a button, and fill in the callback handler blanks.
Especially for a relatively simple toolkit like tkinter, though, a programming-based
approach can often work just as well. We’d like to be able to inherit something that
does all the grunt work of construction for us, given a template for the menus and
toolbars in a window. Here’s one way it can be done—using trees of simple objects.
The class in Example 10-3 interprets data structure representations of menus and tool-
bars and builds all the widgets automatically.
Example 10-3. PP4E\Gui\Tools\guimaker.py
"""
###############################################################################
An extended Frame that makes window menus and toolbars automatically.
Use GuiMakerFrameMenu for embedded components (makes frame-based menus).
Use GuiMakerWindowMenu for top-level windows (makes Tk8.0 window menus).
See the self-test code (and PyEdit) for an example layout tree format.
###############################################################################
"""
import sys
from tkinter import * # widget classes
from tkinter.messagebox import showinfo
class GuiMaker(Frame):
menuBar = [] # class defaults
Figure 10-1. GuiMixin self-test code in action
GuiMaker: Automating Menus and Toolbars | 603