return self.myMenu.items()
def runCommand(self, cmd):
self.myMenu[cmd]()
The ShellGui class in this module knows how to use the GuiMaker and GuiMixin inter-
faces to construct a selection window that displays tool names in menus, a scrolled list,
and a toolbar. It also provides a forToolBar method that you can override and that
allows subclasses to specify which tools should and should not be added to the win-
dow’s toolbar (the toolbar can become crowded in a hurry). However, it is deliberately
ignorant about both the names of tools that should be displayed in those places and
about the actions to be run when tool names are selected.
Instead, ShellGui relies on the ListMenuGui and DictMenuGui subclasses in this file to
provide a list of tool names from a fetchCommands method and dispatch actions by name
in a runCommand method. These two subclasses really just serve to interface to
application-specific tool sets laid out as lists or dictionaries, though; they are still naïve
about what tool names really go up on the GUI. That’s by design, too—because the
tool sets displayed are defined by lower subclasses, we can use ShellGui to display a
variety of different tool sets.
Application-Specific Tool Set Classes
To get to the actual tool sets, we need to go one level down. The module in Exam-
ple 10-6 defines subclasses of the two type-specific ShellGui classes, to provide sets of
available tools in both list and dictionary formats (you would normally need only one,
but this module is meant for illustration). This is also the module that is actually run
to kick off the GUI—the shellgui module is a class library only.
Example 10-6. PP4E\Gui\ShellGui\mytools.py
#!/usr/local/bin/python
"""
################################################################################
provide type-specific option sets for application
################################################################################
"""
from shellgui import * # type-specific option gui
from packdlg import runPackDialog # dialogs for data entry
from unpkdlg import runUnpackDialog # they both run app classes
class TextPak1(ListMenuGui):
def init(self):
self.myMenu = [('Pack ', runPackDialog), # simple functions
('Unpack', runUnpackDialog), # use same width here
('Mtool ', self.notdone)] # method from guimixin
ListMenuGui.init(self)
def forToolBar(self, label):
return label in {'Pack ', 'Unpack'} # 3.x set syntax
ShellGui: GUIs for Command-Line Tools | 615