1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
<!DOCTYPE HTML PUBLIC "-//Netscape_Microsoft//DTD HTML 3.0//EN">
<HTML>
<!-- This file generated using the Python HTMLgen module. -->
<HEAD>
<META NAME="GENERATOR" CONTENT="HTMLgen 1.1">
<TITLE>PmwMenuBar.py</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<PRE>
<FONT COLOR="#DD0000"># Manager widget for menus.</FONT>
import Tkinter
import Pmw
<STRONG><FONT COLOR="#CC6600">class MenuBar</FONT></STRONG>(Pmw.MegaWidget):
<STRONG> def __init__</STRONG>(self, parent = None, **kw):
<FONT COLOR="#DD0000"># Define the megawidget options.</FONT>
INITOPT = Pmw.INITOPT
optiondefs = (
(<FONT COLOR="#009900">'balloon'</FONT>, None, None),
(<FONT COLOR="#009900">'padx'</FONT>, 0, INITOPT),
)
self.defineoptions(kw, optiondefs)
<FONT COLOR="#DD0000"># Initialise the base class (after defining the options).</FONT>
Pmw.MegaWidget.__init__(self, parent)
<FONT COLOR="#DD0000"># Initialise instance variables.</FONT>
self._menuHelpDict = {}
<FONT COLOR="#DD0000"># Check keywords and initialise options.</FONT>
self.initialiseoptions(MenuBar)
<STRONG> def deletemenuitems</STRONG>(self, menuName, start=<FONT COLOR="#009900">'0'</FONT>, end=None):
if (menuName + <FONT COLOR="#009900">'-menu'</FONT>) in self.components():
self.component(menuName + <FONT COLOR="#009900">'-menu'</FONT>).delete(start, end)
<STRONG> def deletemenu</STRONG>(self, menuName):
<FONT COLOR="#0000DD">"""Delete should be called for cascaded menus before main menus.
"""</FONT>
if (menuName + <FONT COLOR="#009900">'-menu'</FONT>) in self.components():
self.destroycomponent(menuName + <FONT COLOR="#009900">'-menu'</FONT>)
if (menuName + <FONT COLOR="#009900">'-button'</FONT>) in self.components():
self.destroycomponent(menuName + <FONT COLOR="#009900">'-button'</FONT>)
if self._menuHelpDict.has_key(menuName + <FONT COLOR="#009900">'-menu'</FONT>):
del self._menuHelpDict[menuName + <FONT COLOR="#009900">'-menu'</FONT>]
<STRONG> def disableall</STRONG>(self):
for item in self.components():
if len(item) > 6 and item[-6:] == <FONT COLOR="#009900">'button'</FONT>:
self.component(item).configure(state=<FONT COLOR="#009900">'disabled'</FONT>)
<STRONG> def enableall</STRONG>(self):
for item in self.components():
if len(item) > 6 and item[-6:] == <FONT COLOR="#009900">'button'</FONT>:
self.component(item).configure(state=<FONT COLOR="#009900">'normal'</FONT>)
<STRONG> def addcascademenu</STRONG>(self, menuName, submenu, help=<FONT COLOR="#009900">''</FONT>, **kw):
if (submenu + <FONT COLOR="#009900">'-menu'</FONT>) in self.components():
raise ValueError, <FONT COLOR="#009900">'submenu "%s" already exists'</FONT> % submenu
menu = self.createcomponent(submenu + <FONT COLOR="#009900">'-menu'</FONT>,
(), <FONT COLOR="#009900">'Menu'</FONT>,
Tkinter.Menu,(self.component(menuName + <FONT COLOR="#009900">'-menu'</FONT>),), tearoff=0)
self._menuHelpDict[submenu] = []
kw[<FONT COLOR="#009900">'menu'</FONT>] = menu
if not kw.has_key(<FONT COLOR="#009900">'label'</FONT>):
kw[<FONT COLOR="#009900">'label'</FONT>] = submenu
apply(self.addmenuitem, (menuName, <FONT COLOR="#009900">'cascade'</FONT>, help), kw)
<FONT COLOR="#DD0000"># Need to put this binding after the class bindings so that</FONT>
<FONT COLOR="#DD0000"># menu.index() does not lag behind.</FONT>
_bindtag = <FONT COLOR="#009900">'PmwMenuBar'</FONT> + str(self) + submenu
self.bind_class(_bindtag, <FONT COLOR="#009900">'<Motion>'</FONT>,
lambda event=None, self=self, menuName=submenu:
self._menuHelp(menuName))
menu.bindtags(menu.bindtags() + (_bindtag,))
menu.bind(<FONT COLOR="#009900">'<Leave>'</FONT>, self._resetHelpmessage)
<STRONG> def addmenu</STRONG>(self, menuName, balloonHelp, statusHelp=None,
side=<FONT COLOR="#009900">'left'</FONT>, **kw):
if (menuName + <FONT COLOR="#009900">'-button'</FONT>) in self.components():
raise ValueError, <FONT COLOR="#009900">'menu "%s" already exists'</FONT> % menuName
if not kw.has_key(<FONT COLOR="#009900">'text'</FONT>):
kw[<FONT COLOR="#009900">'text'</FONT>] = menuName
button = apply(self.createcomponent, (menuName + <FONT COLOR="#009900">'-button'</FONT>,
(), <FONT COLOR="#009900">'Button'</FONT>,
Tkinter.Menubutton, (self.interior(),)), kw)
button.pack(side=side, padx = self[<FONT COLOR="#009900">'padx'</FONT>])
balloon = self[<FONT COLOR="#009900">'balloon'</FONT>]
if balloon is not None:
balloon.bind(button, balloonHelp, statusHelp)
menu = self.createcomponent(menuName + <FONT COLOR="#009900">'-menu'</FONT>,
(), <FONT COLOR="#009900">'Menu'</FONT>,
Tkinter.Menu, (button,), tearoff=0)
button.configure(menu = menu)
self._menuHelpDict[menuName] = []
<FONT COLOR="#DD0000"># Need to put this binding after the class bindings so that</FONT>
<FONT COLOR="#DD0000"># menu.index() does not lag behind.</FONT>
_bindtag = <FONT COLOR="#009900">'PmwMenuBar'</FONT> + str(self) + menuName
self.bind_class(_bindtag, <FONT COLOR="#009900">'<Motion>'</FONT>,
lambda event=None, self=self, menuName=menuName:
self._menuHelp(menuName))
menu.bindtags(menu.bindtags() + (_bindtag,))
menu.bind(<FONT COLOR="#009900">'<Leave>'</FONT>, self._resetHelpmessage)
return button
<STRONG> def addmenuitem</STRONG>(self, menuName, type, help=<FONT COLOR="#009900">''</FONT>, **kw):
menu = self.component(menuName + <FONT COLOR="#009900">'-menu'</FONT>)
if type == <FONT COLOR="#009900">'command'</FONT>:
command = menu.add_command
elif type == <FONT COLOR="#009900">'separator'</FONT>:
command = menu.add_separator
elif type == <FONT COLOR="#009900">'checkbutton'</FONT>:
command = menu.add_checkbutton
elif type == <FONT COLOR="#009900">'radiobutton'</FONT>:
command = menu.add_radiobutton
elif type == <FONT COLOR="#009900">'cascade'</FONT>:
command = menu.add_cascade
self._menuHelpDict[menuName].append(help)
apply(command, (), kw)
<STRONG> def _menuHelp</STRONG>(self, menuName):
menu = self.component(menuName + <FONT COLOR="#009900">'-menu'</FONT>)
index = menu.index(<FONT COLOR="#009900">'active'</FONT>)
if index is None:
self._resetHelpmessage()
else:
help = self._menuHelpDict[menuName][index]
balloon = self[<FONT COLOR="#009900">'balloon'</FONT>]
if balloon is not None:
balloon.showstatus(help)
<STRONG> def _resetHelpmessage</STRONG>(self, event=None):
balloon = self[<FONT COLOR="#009900">'balloon'</FONT>]
if balloon is not None:
balloon.clearstatus()
</PRE>
</BODY> </HTML>
|