def start(self): # use GuiMaker if component
self.setMenuBar()
self.setToolBar()
self.master.title("Shell Tools Listbox")
self.master.iconname("Shell Tools")
def handleList(self, event): # on listbox double-click
label = self.listbox.get(ACTIVE) # fetch selection text
self.runCommand(label) # and call action here
def makeWidgets(self): # add listbox in middle
sbar = Scrollbar(self) # cross link sbar, list
list = Listbox(self, bg='white') # or use Tour.ScrolledList
sbar.config(command=list.yview)
list.config(yscrollcommand=sbar.set)
sbar.pack(side=RIGHT, fill=Y) # pack 1st=clip last
list.pack(side=LEFT, expand=YES, fill=BOTH) # list clipped first
for (label, action) in self.fetchCommands(): # add to listbox
list.insert(END, label) # and menu/toolbars
list.bind('
self.listbox = list
def forToolBar(self, label): # put on toolbar?
return True # default = all
def setToolBar(self):
self.toolBar = []
for (label, action) in self.fetchCommands():
if self.forToolBar(label):
self.toolBar.append((label, action, dict(side=LEFT)))
self.toolBar.append(('Quit', self.quit, dict(side=RIGHT)))
def setMenuBar(self):
toolEntries = []
self.menuBar = [
('File', 0, [('Quit', −1, self.quit)]), # pull-down name
('Tools', 0, toolEntries) # menu items list
] # label,underline,action
for (label, action) in self.fetchCommands():
toolEntries.append((label, −1, action)) # add app items to menu
################################################################################
delegate to template type-specific subclasses
which delegate to app tool-set-specific subclasses
################################################################################
class ListMenuGui(ShellGui):
def fetchCommands(self): # subclass: set 'myMenu'
return self.myMenu # list of (label, callback)
def runCommand(self, cmd):
for (label, action) in self.myMenu:
if label == cmd: action()
class DictMenuGui(ShellGui):
def fetchCommands(self):
614 | Chapter 10: GUI Coding Techniques