# Manager widget for menus. import Tkinter import Pmw class MenuBar(Pmw.MegaWidget): def __init__(self, parent = None, **kw): # Define the megawidget options. INITOPT = Pmw.INITOPT optiondefs = ( ('balloon', None, None), ('padx', 0, INITOPT), ) self.defineoptions(kw, optiondefs) # Initialise the base class (after defining the options). Pmw.MegaWidget.__init__(self, parent) # Initialise instance variables. self._menuHelpDict = {} # Check keywords and initialise options. self.initialiseoptions(MenuBar) def deletemenuitems(self, menuName, start='0', end=None): if (menuName + '-menu') in self.components(): self.component(menuName + '-menu').delete(start, end) def deletemenu(self, menuName): """Delete should be called for cascaded menus before main menus. """ if (menuName + '-menu') in self.components(): self.destroycomponent(menuName + '-menu') if (menuName + '-button') in self.components(): self.destroycomponent(menuName + '-button') if self._menuHelpDict.has_key(menuName + '-menu'): del self._menuHelpDict[menuName + '-menu'] def disableall(self): for item in self.components(): if len(item) > 6 and item[-6:] == 'button': self.component(item).configure(state='disabled') def enableall(self): for item in self.components(): if len(item) > 6 and item[-6:] == 'button': self.component(item).configure(state='normal') def addcascademenu(self, menuName, submenu, help='', **kw): if (submenu + '-menu') in self.components(): raise ValueError, 'submenu "%s" already exists' % submenu menu = self.createcomponent(submenu + '-menu', (), 'Menu', Tkinter.Menu,(self.component(menuName + '-menu'),), tearoff=0) self._menuHelpDict[submenu] = [] kw['menu'] = menu if not kw.has_key('label'): kw['label'] = submenu apply(self.addmenuitem, (menuName, 'cascade', help), kw) # Need to put this binding after the class bindings so that # menu.index() does not lag behind. _bindtag = 'PmwMenuBar' + str(self) + submenu self.bind_class(_bindtag, '<Motion>', lambda event=None, self=self, menuName=submenu: self._menuHelp(menuName)) menu.bindtags(menu.bindtags() + (_bindtag,)) menu.bind('<Leave>', self._resetHelpmessage) def addmenu(self, menuName, balloonHelp, statusHelp=None, side='left', **kw): if (menuName + '-button') in self.components(): raise ValueError, 'menu "%s" already exists' % menuName if not kw.has_key('text'): kw['text'] = menuName button = apply(self.createcomponent, (menuName + '-button', (), 'Button', Tkinter.Menubutton, (self.interior(),)), kw) button.pack(side=side, padx = self['padx']) balloon = self['balloon'] if balloon is not None: balloon.bind(button, balloonHelp, statusHelp) menu = self.createcomponent(menuName + '-menu', (), 'Menu', Tkinter.Menu, (button,), tearoff=0) button.configure(menu = menu) self._menuHelpDict[menuName] = [] # Need to put this binding after the class bindings so that # menu.index() does not lag behind. _bindtag = 'PmwMenuBar' + str(self) + menuName self.bind_class(_bindtag, '<Motion>', lambda event=None, self=self, menuName=menuName: self._menuHelp(menuName)) menu.bindtags(menu.bindtags() + (_bindtag,)) menu.bind('<Leave>', self._resetHelpmessage) return button def addmenuitem(self, menuName, type, help='', **kw): menu = self.component(menuName + '-menu') if type == 'command': command = menu.add_command elif type == 'separator': command = menu.add_separator elif type == 'checkbutton': command = menu.add_checkbutton elif type == 'radiobutton': command = menu.add_radiobutton elif type == 'cascade': command = menu.add_cascade self._menuHelpDict[menuName].append(help) apply(command, (), kw) def _menuHelp(self, menuName): menu = self.component(menuName + '-menu') index = menu.index('active') if index is None: self._resetHelpmessage() else: help = self._menuHelpDict[menuName][index] balloon = self['balloon'] if balloon is not None: balloon.showstatus(help) def _resetHelpmessage(self, event=None): balloon = self['balloon'] if balloon is not None: balloon.clearstatus()