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>PmwOptionMenu.py</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<PRE>
<FONT COLOR="#DD0000">#</FONT>
<FONT COLOR="#DD0000"># Widget implementing functionality similar to that of OptionMenu widget</FONT>
<FONT COLOR="#DD0000">#</FONT>
<FONT COLOR="#DD0000"># The widget defines two components: label and menu, menu being Tkinter's </FONT>
<FONT COLOR="#DD0000"># OptionMenu</FONT>
<FONT COLOR="#DD0000">#</FONT>
<FONT COLOR="#DD0000"># The public methods are:</FONT>
<FONT COLOR="#DD0000">#</FONT>
<FONT COLOR="#DD0000"># get() - returns the current selection</FONT>
# index(name) - returns the integer index of the item <FONT COLOR="#009900">'name'</FONT>
<FONT COLOR="#DD0000"># setitems(items, active = None)</FONT>
<FONT COLOR="#DD0000"># - set's the list of items displayed, selects the item indexed</FONT>
# by <FONT COLOR="#009900">'active'</FONT> as active (calls the callback too). Otherwise,
<FONT COLOR="#DD0000"># first item in the list is selected.</FONT>
<FONT COLOR="#DD0000"># select(index) - selects the item indexed by index. If command option is given,</FONT>
<FONT COLOR="#DD0000"># the command is called</FONT>
<FONT COLOR="#DD0000">#</FONT>
# If an option <FONT COLOR="#009900">'variable'</FONT> is given, the current value of the menu will be
<FONT COLOR="#DD0000"># available via the variable.</FONT>
<FONT COLOR="#DD0000">#</FONT>
<FONT COLOR="#DD0000"># Author Roman Sulzhyk</FONT>
<FONT COLOR="#DD0000">#</FONT>
import Tkinter, Pmw
import string
<FONT COLOR="#DD0000"># Dummy callback</FONT>
<STRONG><FONT COLOR="#CC6600">class _dummy</FONT></STRONG>:
<STRONG> def __init__</STRONG>(self, f, pars):
self.f, self.pars = f, pars
<STRONG> def __call__</STRONG>(self):
apply ( self.f, (self.pars, ) )
<STRONG><FONT COLOR="#CC6600">class OptionMenu</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">'labelmargin'</FONT>, 0, INITOPT),
(<FONT COLOR="#009900">'labelpos'</FONT>, None, INITOPT),
(<FONT COLOR="#009900">'selectioncommand'</FONT>, <FONT COLOR="#009900">''</FONT>, None),
(<FONT COLOR="#009900">'items'</FONT>, [], INITOPT ),
(<FONT COLOR="#009900">'variable'</FONT>, Tkinter.StringVar(<FONT COLOR="#009900">''</FONT>), INITOPT ),
(<FONT COLOR="#009900">'command'</FONT>, None, None ),
(<FONT COLOR="#009900">'direction'</FONT>, <FONT COLOR="#009900">'flush'</FONT>, 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"># Check that the direction is valid</FONT>
dirs = [ <FONT COLOR="#009900">'left'</FONT>, <FONT COLOR="#009900">'right'</FONT>, <FONT COLOR="#009900">'above'</FONT>, <FONT COLOR="#009900">'below'</FONT>, <FONT COLOR="#009900">'flush'</FONT> ]
if self[<FONT COLOR="#009900">'direction'</FONT>] not in dirs:
raise ValueError, <FONT COLOR="#009900">'direction is %s, should be one of : %s'</FONT> % \
( self[<FONT COLOR="#009900">'direction'</FONT>], string.join ( dirs, <FONT COLOR="#009900">', '</FONT> ))
<FONT COLOR="#DD0000"># A nuisance inherited from OptionMenu</FONT>
if not len(self[<FONT COLOR="#009900">'items'</FONT>]):
raise ValueError, <FONT COLOR="#009900">'items must be a non-empty list'</FONT>
interior = self.interior()
<FONT COLOR="#DD0000"># Create OptionMenu</FONT>
self._optionmenu = self.createcomponent(<FONT COLOR="#009900">'menu'</FONT>,
(), None,
Tkinter.OptionMenu, (interior, self[<FONT COLOR="#009900">'variable'</FONT>],
self[<FONT COLOR="#009900">'items'</FONT>][0], self[<FONT COLOR="#009900">'items'</FONT>][1:]),
)
self._optionmenu.configure ( direction = self[<FONT COLOR="#009900">'direction'</FONT>],)
self._optionmenu.grid ( row = 2, column = 2, sticky=<FONT COLOR="#009900">'nsew'</FONT>)
interior.grid_columnconfigure ( 2, weight = 1)
interior.grid_rowconfigure ( 2, weight = 1)
<FONT COLOR="#DD0000"># Create the label.</FONT>
self.createlabel(interior)
<FONT COLOR="#DD0000"># Re-set the items to register proper callback</FONT>
self.setitems ( self[<FONT COLOR="#009900">'items'</FONT>] )
<FONT COLOR="#DD0000"># Check keywords and initialise options.</FONT>
self.initialiseoptions(OptionMenu)
<FONT COLOR="#DD0000">#======================================================================</FONT>
<FONT COLOR="#DD0000"># Public methods</FONT>
<STRONG> def get</STRONG> ( self ):
"Return the name of the menu item currently selected"
return self[<FONT COLOR="#009900">'variable'</FONT>].get()
<STRONG> def setitems</STRONG> ( self, items, active = None ):
"Set the list of items to be displayed"
m = self._optionmenu[<FONT COLOR="#009900">'menu'</FONT>]
m.delete ( 0, <FONT COLOR="#009900">'end'</FONT> )
for i in items:
m.add_command ( label = i, command = _dummy ( self._callback, i ))
if active:
self.select(active)
else:
self.select(items[0])
<STRONG> def index</STRONG> ( self, name ):
"Return integer index of item with name <FONT COLOR="#009900">'name'</FONT>"
return self._optionmenu[<FONT COLOR="#009900">'menu'</FONT>].index(name)
<STRONG> def select</STRONG> ( self, index ):
"Select the item with index"
ind = self.index ( index )
val = self._optionmenu[<FONT COLOR="#009900">'menu'</FONT>].entrycget(ind, <FONT COLOR="#009900">'label'</FONT>)
self[<FONT COLOR="#009900">'variable'</FONT>].set(val)
<FONT COLOR="#DD0000"># Note that the callback is called:</FONT>
if callable ( self[<FONT COLOR="#009900">'command'</FONT>] ):
apply ( self[<FONT COLOR="#009900">'command'</FONT>], (val, ) )
<FONT COLOR="#DD0000">#======================================================================</FONT>
<FONT COLOR="#DD0000"># Private methods</FONT>
<STRONG> def _callback</STRONG> ( self, tag ):
"Called when user selects an item"
self[<FONT COLOR="#009900">'variable'</FONT>].set(tag)
if callable ( self[<FONT COLOR="#009900">'command'</FONT>] ):
apply ( self[<FONT COLOR="#009900">'command'</FONT>], (tag, ) )
</PRE>
</BODY> </HTML>
|