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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
<!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>PmwButtonBox.py</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<PRE>
<FONT COLOR="#DD0000"># Based on iwidgets2.2.0/buttonbox.itk code.</FONT>
import Tkinter
import Pmw
<STRONG><FONT COLOR="#CC6600">class ButtonBox</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">'orient'</FONT>, <FONT COLOR="#009900">'horizontal'</FONT>, INITOPT),
(<FONT COLOR="#009900">'padx'</FONT>, 8, INITOPT),
(<FONT COLOR="#009900">'pady'</FONT>, 8, INITOPT),
(<FONT COLOR="#009900">'ringborderwidth'</FONT>, 1, INITOPT),
(<FONT COLOR="#009900">'ringpadx'</FONT>, 1, INITOPT),
(<FONT COLOR="#009900">'ringpady'</FONT>, 1, 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"># Create the components.</FONT>
interior = self.interior()
if self[<FONT COLOR="#009900">'labelpos'</FONT>] is None:
self._buttonBoxFrame = self._hull
columnOrRow = 0
else:
self._buttonBoxFrame = self.createcomponent(<FONT COLOR="#009900">'frame'</FONT>,
(), None,
Tkinter.Frame, (interior,))
self._buttonBoxFrame.grid(column=2, row=2, sticky=<FONT COLOR="#009900">'nsew'</FONT>)
columnOrRow = 2
self.createlabel(interior)
if self[<FONT COLOR="#009900">'orient'</FONT>] == <FONT COLOR="#009900">'horizontal'</FONT>:
interior.grid_columnconfigure(columnOrRow, weight = 1)
else:
interior.grid_rowconfigure(columnOrRow, weight = 1)
<FONT COLOR="#DD0000"># Initialise instance variables.</FONT>
<FONT COLOR="#DD0000"># List of tuples describing the buttons:</FONT>
<FONT COLOR="#DD0000"># - name</FONT>
<FONT COLOR="#DD0000"># - button widget</FONT>
<FONT COLOR="#DD0000"># - surrounding frame widget</FONT>
self._buttonList = []
<FONT COLOR="#DD0000"># The index of the default button.</FONT>
self._defaultButton = None
self._timerId = None
<FONT COLOR="#DD0000"># Check keywords and initialise options.</FONT>
self.initialiseoptions(ButtonBox)
<STRONG> def destroy</STRONG>(self):
if self._timerId:
self.after_cancel(self._timerId)
self._timerId = None
Pmw.MegaWidget.destroy(self)
<STRONG> def numbuttons</STRONG>(self):
return len(self._buttonList)
<STRONG> def index</STRONG>(self, index, forInsert = 0):
listLength = len(self._buttonList)
if type(index) == type(1):
if forInsert and index <= listLength:
return index
elif not forInsert and index < listLength:
return index
else:
raise ValueError, <FONT COLOR="#009900">'index "%s" is out of range'</FONT> % index
elif index == <FONT COLOR="#009900">'end'</FONT>:
if forInsert:
return listLength
elif listLength > 0:
return listLength - 1
else:
raise ValueError, <FONT COLOR="#009900">'ButtonBox has no buttons'</FONT>
elif index == <FONT COLOR="#009900">'default'</FONT>:
if self._defaultButton is not None:
return self._defaultButton
raise ValueError, <FONT COLOR="#009900">'ButtonBox has no default'</FONT>
else:
for count in range(listLength):
name = self._buttonList[count][0]
if index == name:
return count
validValues = <FONT COLOR="#009900">'number, end, default, or a name'</FONT>
raise ValueError, \
<FONT COLOR="#009900">'bad index "%s": must be %s'</FONT> % (index, validValues)
<STRONG> def insert</STRONG>(self, name, before = 0, **kw):
if name in self.components():
raise ValueError, <FONT COLOR="#009900">'button "%s" already exists'</FONT> % name
frame = Tkinter.Frame(self._buttonBoxFrame,
borderwidth = self[<FONT COLOR="#009900">'ringborderwidth'</FONT>], relief = <FONT COLOR="#009900">'flat'</FONT>)
if not kw.has_key(<FONT COLOR="#009900">'text'</FONT>):
kw[<FONT COLOR="#009900">'text'</FONT>] = name
button = apply(self.createcomponent, (name,
(), <FONT COLOR="#009900">'Button'</FONT>,
Tkinter.Button, (frame,)), kw)
button.pack(padx = self[<FONT COLOR="#009900">'ringpadx'</FONT>], pady = self[<FONT COLOR="#009900">'ringpady'</FONT>],
fill = <FONT COLOR="#009900">'both'</FONT>, expand = 1)
index = self.index(before, 1)
horizontal = self[<FONT COLOR="#009900">'orient'</FONT>] == <FONT COLOR="#009900">'horizontal'</FONT>
numButtons = len(self._buttonList)
<FONT COLOR="#DD0000"># Shift buttons up one position.</FONT>
for i in range(numButtons - 1, index - 1, -1):
widget = self._buttonList[i][2]
pos = i * 2 + 3
if horizontal:
widget.grid(column = pos, row = 0)
else:
widget.grid(column = 0, row = pos)
<FONT COLOR="#DD0000"># Display the new button.</FONT>
if horizontal:
frame.grid(column = index * 2 + 1, row = 0, sticky = <FONT COLOR="#009900">'ew'</FONT>,
padx = self[<FONT COLOR="#009900">'padx'</FONT>], pady = self[<FONT COLOR="#009900">'pady'</FONT>])
self._buttonBoxFrame.grid_columnconfigure(numButtons * 2 + 2, weight = 1)
else:
frame.grid(column = 0, row = index * 2 + 1, sticky = <FONT COLOR="#009900">'ew'</FONT>,
padx = self[<FONT COLOR="#009900">'padx'</FONT>], pady = self[<FONT COLOR="#009900">'pady'</FONT>])
self._buttonBoxFrame.grid_rowconfigure(numButtons * 2 + 2, weight = 1)
self._buttonList.insert(index, (name, button, frame))
return button
<STRONG> def add</STRONG>(self, name, **kw):
return apply(self.insert, (name, len(self._buttonList)), kw)
<STRONG> def delete</STRONG>(self, index):
index = self.index(index)
(name, widget, frame) = self._buttonList[index]
frame.grid_forget()
self.destroycomponent(name)
numButtons = len(self._buttonList)
<FONT COLOR="#DD0000"># Shift buttons down one position.</FONT>
horizontal = self[<FONT COLOR="#009900">'orient'</FONT>] == <FONT COLOR="#009900">'horizontal'</FONT>
for i in range(index + 1, numButtons):
frame = self._buttonList[i][2]
pos = i * 2 - 1
if horizontal:
frame.grid(column = pos, row = 0)
else:
frame.grid(column = 0, row = pos)
if horizontal:
self._buttonBoxFrame.grid_columnconfigure(numButtons * 2 - 1,
minsize = 0)
self._buttonBoxFrame.grid_columnconfigure(numButtons * 2, weight = 0)
else:
self._buttonBoxFrame.grid_rowconfigure(numButtons * 2, weight = 0)
del self._buttonList[index]
<STRONG> def setdefault</STRONG>(self, index):
<FONT COLOR="#DD0000"># Turn off the default ring around the current default button.</FONT>
if self._defaultButton is not None:
frame = self._buttonList[self._defaultButton][2]
frame.configure(relief = <FONT COLOR="#009900">'flat'</FONT>)
self._defaultButton = None
<FONT COLOR="#DD0000"># Turn on the default ring around the new default button.</FONT>
if index is not None:
index = self.index(index)
self._defaultButton = index
frame = self._buttonList[index][2]
frame.configure(relief = <FONT COLOR="#009900">'sunken'</FONT>)
<STRONG> def invoke</STRONG>(self, index = <FONT COLOR="#009900">'default'</FONT>, noFlash = 0):
<FONT COLOR="#DD0000"># Invoke the callback associated with the *index* button. If</FONT>
<FONT COLOR="#DD0000"># *noFlash* is not set, flash the button to indicate to the</FONT>
<FONT COLOR="#DD0000"># user that something happened.</FONT>
button = self._buttonList[self.index(index)][1]
if not noFlash:
state = button.cget(<FONT COLOR="#009900">'state'</FONT>)
relief = button.cget(<FONT COLOR="#009900">'relief'</FONT>)
button.configure(state = <FONT COLOR="#009900">'active'</FONT>, relief = <FONT COLOR="#009900">'sunken'</FONT>)
self.update_idletasks()
self.after(100)
button.configure(state = state, relief = relief)
return button.invoke()
<STRONG> def alignbuttons</STRONG>(self, when = <FONT COLOR="#009900">'later'</FONT>):
if when == <FONT COLOR="#009900">'later'</FONT>:
if not self._timerId:
self._timerId = self.after_idle(self.alignbuttons, <FONT COLOR="#009900">'now'</FONT>)
return
self.update_idletasks()
self._timerId = None
if self[<FONT COLOR="#009900">'orient'</FONT>] == <FONT COLOR="#009900">'horizontal'</FONT>:
<FONT COLOR="#DD0000"># Determine the width of the maximum length button.</FONT>
max = 0
for index in range(len(self._buttonList)):
width = self._buttonBoxFrame.grid_bbox(index * 2 + 1, 0)[2]
if width > max:
max = width
<FONT COLOR="#DD0000"># Set the widths of all the buttons to be the same.</FONT>
for index in range(len(self._buttonList)):
self._buttonBoxFrame.grid_columnconfigure(index * 2 + 1,
minsize = max)
else:
<FONT COLOR="#DD0000"># Determine the width of the maximum length button.</FONT>
max = 0
for index in range(len(self._buttonList)):
width = self._buttonBoxFrame.grid_bbox(0, index * 2 + 1)[2]
if width > max:
max = width
<FONT COLOR="#DD0000"># Set the width of all the buttons to be the same.</FONT>
self._buttonBoxFrame.grid_columnconfigure(0, minsize = max)
</PRE>
</BODY> </HTML>
|