File: PmwComboBox.py.html

package info (click to toggle)
python-pmw 0.6.2-0.1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,652 kB
  • ctags: 2,716
  • sloc: python: 10,720; makefile: 44; sh: 24
file content (397 lines) | stat: -rw-r--r-- 16,446 bytes parent folder | download
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<!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>PmwComboBox.py</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<PRE>
<FONT COLOR="#DD0000"># Based on iwidgets2.2.0/combobox.itk code.</FONT>

import os
import string
import types
import Tkinter
import Pmw

<STRONG><FONT COLOR="#CC6600">class ComboBox</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">'arrowrelief'</FONT>,        <FONT COLOR="#009900">'raised'</FONT>,   INITOPT),
	    (<FONT COLOR="#009900">'autoclear'</FONT>,          0,          INITOPT),
	    (<FONT COLOR="#009900">'buttonaspect'</FONT>,       1.0,        INITOPT),
	    (<FONT COLOR="#009900">'dropdown'</FONT>,           1,          INITOPT),
	    (<FONT COLOR="#009900">'fliparrow'</FONT>,          0,          INITOPT),
	    (<FONT COLOR="#009900">'history'</FONT>,            1,          INITOPT),
	    (<FONT COLOR="#009900">'labelmargin'</FONT>,        0,          INITOPT),
	    (<FONT COLOR="#009900">'labelpos'</FONT>,           None,       INITOPT),
	    (<FONT COLOR="#009900">'listheight'</FONT>,         150,        INITOPT),
	    (<FONT COLOR="#009900">'selectioncommand'</FONT>,   <FONT COLOR="#009900">''</FONT>,         None),
	    (<FONT COLOR="#009900">'unique'</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()

	self._entryfield = self.createcomponent(<FONT COLOR="#009900">'entryfield'</FONT>,
		((<FONT COLOR="#009900">'entry'</FONT>, <FONT COLOR="#009900">'entryfield_entry'</FONT>),), None,
		Pmw.EntryField, (interior,))
	self._entryfield.grid(column=2, row=2, sticky=<FONT COLOR="#009900">'nsew'</FONT>)
	interior.grid_columnconfigure(2, weight = 1)
	interior.grid_rowconfigure(2, weight = 1)
	self._entryWidget = self._entryfield.component(<FONT COLOR="#009900">'entry'</FONT>)

	if self[<FONT COLOR="#009900">'dropdown'</FONT>]:
	    <FONT COLOR="#DD0000"># This is needed to protect against _postList being called</FONT>
	    <FONT COLOR="#DD0000"># recursively (via the call to wait_visibility) if the user</FONT>
	    <FONT COLOR="#DD0000"># clicks on the button quickly.</FONT>
	    self._isPosted = 0

	    <FONT COLOR="#DD0000"># Create the arrow button.</FONT>
	    self._arrowBtn = self.createcomponent(<FONT COLOR="#009900">'arrowbutton'</FONT>,
		    (), None,
		    Tkinter.Canvas, (interior,), borderwidth = 2,
		    relief = self[<FONT COLOR="#009900">'arrowrelief'</FONT>],
		    width = 16, height = 16)
	    self._arrowBtn.grid(column=3, row=2)

	    <FONT COLOR="#DD0000"># Create the label.</FONT>
	    self.createlabel(interior, childCols=2)

	    <FONT COLOR="#DD0000"># Create the dropdown window.</FONT>
	    self._popup = self.createcomponent(<FONT COLOR="#009900">'popup'</FONT>,
		    (), None,
		    Tkinter.Toplevel, (interior,))
	    self._popup.withdraw()
	    self._popup.overrideredirect(1)

	    <FONT COLOR="#DD0000"># Create the scrolled listbox inside the dropdown window.</FONT>
	    self._list = self.createcomponent(<FONT COLOR="#009900">'scrolledlist'</FONT>,
		    ((<FONT COLOR="#009900">'listbox'</FONT>, <FONT COLOR="#009900">'scrolledlist_listbox'</FONT>),), None,
		    Pmw.ScrolledListBox, (self._popup,),
		    hull_borderwidth = 2,
		    hull_relief = <FONT COLOR="#009900">'raised'</FONT>,
		    hull_height = self[<FONT COLOR="#009900">'listheight'</FONT>],
		    listbox_width = 1,
		    listbox_height = 1,
		    listbox_exportselection = 0)
	    self._list.pack(expand=1, fill=<FONT COLOR="#009900">'both'</FONT>)
	    self._list.grid_propagate(0)
	    self.__listbox = self._list.component(<FONT COLOR="#009900">'listbox'</FONT>)

	    <FONT COLOR="#DD0000"># Bind events to the arrow button.</FONT>
	    self._arrowBtn.bind(<FONT COLOR="#009900">'&lt;1&gt;'</FONT>, self._postList)
	    self._arrowBtn.bind(<FONT COLOR="#009900">'&lt;Configure&gt;'</FONT>, self._drawArrow)
	    self._arrowBtn.bind(<FONT COLOR="#009900">'&lt;3&gt;'</FONT>, self._next)
	    self._arrowBtn.bind(<FONT COLOR="#009900">'&lt;Shift-3&gt;'</FONT>, self._previous)
	    self._arrowBtn.bind(<FONT COLOR="#009900">'&lt;Down&gt;'</FONT>, self._next)
	    self._arrowBtn.bind(<FONT COLOR="#009900">'&lt;Up&gt;'</FONT>, self._previous)
	    self._arrowBtn.bind(<FONT COLOR="#009900">'&lt;Control-n&gt;'</FONT>, self._next)
	    self._arrowBtn.bind(<FONT COLOR="#009900">'&lt;Control-p&gt;'</FONT>, self._previous)
	    self._arrowBtn.bind(<FONT COLOR="#009900">'&lt;Shift-Down&gt;'</FONT>, self._postList)
	    self._arrowBtn.bind(<FONT COLOR="#009900">'&lt;Shift-Up&gt;'</FONT>, self._postList)
	    self._arrowBtn.bind(<FONT COLOR="#009900">'&lt;F34&gt;'</FONT>, self._postList)
	    self._arrowBtn.bind(<FONT COLOR="#009900">'&lt;F28&gt;'</FONT>, self._postList)
	    self._arrowBtn.bind(<FONT COLOR="#009900">'&lt;Return&gt;'</FONT>, self._postList)
	    self._arrowBtn.bind(<FONT COLOR="#009900">'&lt;space&gt;'</FONT>, self._postList)

	    <FONT COLOR="#DD0000"># Bind events to the dropdown window.</FONT>
	    self._popup.bind(<FONT COLOR="#009900">'&lt;Escape&gt;'</FONT>, self._unpostList)
	    self._popup.bind(<FONT COLOR="#009900">'&lt;space&gt;'</FONT>, self._selectUnpost)
	    self._popup.bind(<FONT COLOR="#009900">'&lt;Return&gt;'</FONT>, self._selectUnpost)
	    self._popup.bind(<FONT COLOR="#009900">'&lt;ButtonRelease-1&gt;'</FONT>, self._dropdownBtnRelease)
	    self._popup.bind(<FONT COLOR="#009900">'&lt;ButtonPress-1&gt;'</FONT>, self._unpostOnNextRelease)

	    <FONT COLOR="#DD0000"># Bind events to the Tk listbox.</FONT>
	    self.__listbox.bind(<FONT COLOR="#009900">'&lt;Enter&gt;'</FONT>, self._unpostOnNextRelease)

	    <FONT COLOR="#DD0000"># Bind events to the Tk entry widget.</FONT>
	    self._entryWidget.bind(<FONT COLOR="#009900">'&lt;Configure&gt;'</FONT>, self._resizeArrow)
	    self._entryWidget.bind(<FONT COLOR="#009900">'&lt;Shift-Down&gt;'</FONT>, self._postList)
	    self._entryWidget.bind(<FONT COLOR="#009900">'&lt;Shift-Up&gt;'</FONT>, self._postList)
	    self._entryWidget.bind(<FONT COLOR="#009900">'&lt;F34&gt;'</FONT>, self._postList)
	    self._entryWidget.bind(<FONT COLOR="#009900">'&lt;F28&gt;'</FONT>, self._postList)

	else:
	    <FONT COLOR="#DD0000"># Create the scrolled listbox below the entry field.</FONT>
	    self._list = self.createcomponent(<FONT COLOR="#009900">'scrolledlist'</FONT>,
		    ((<FONT COLOR="#009900">'listbox'</FONT>, <FONT COLOR="#009900">'scrolledlist_listbox'</FONT>),), None,
		    Pmw.ScrolledListBox, (interior,))
	    self._list.grid(column=2, row=3, sticky=<FONT COLOR="#009900">'nsew'</FONT>)
	    self.__listbox = self._list.component(<FONT COLOR="#009900">'listbox'</FONT>)

	    <FONT COLOR="#DD0000"># The scrolled listbox should expand vertically.</FONT>
	    interior.grid_rowconfigure(3, weight = 1)

	    <FONT COLOR="#DD0000"># Create the label.</FONT>
	    self.createlabel(interior, childRows=2)

	    <FONT COLOR="#DD0000"># Bind events to the Tk listbox.</FONT>
	    self.__listbox.bind(<FONT COLOR="#009900">'&lt;ButtonRelease-1&gt;'</FONT>, self._simpleBtnRelease)
	    self.__listbox.bind(<FONT COLOR="#009900">'&lt;space&gt;'</FONT>, self._selectCmd)
	    self.__listbox.bind(<FONT COLOR="#009900">'&lt;Return&gt;'</FONT>, self._selectCmd)

	self._entryWidget.bind(<FONT COLOR="#009900">'&lt;Down&gt;'</FONT>, self._next)
	self._entryWidget.bind(<FONT COLOR="#009900">'&lt;Up&gt;'</FONT>, self._previous)
	self._entryWidget.bind(<FONT COLOR="#009900">'&lt;Control-n&gt;'</FONT>, self._next)
	self._entryWidget.bind(<FONT COLOR="#009900">'&lt;Control-p&gt;'</FONT>, self._previous)
	self.__listbox.bind(<FONT COLOR="#009900">'&lt;Control-n&gt;'</FONT>, self._next)
	self.__listbox.bind(<FONT COLOR="#009900">'&lt;Control-p&gt;'</FONT>, self._previous)

	if self[<FONT COLOR="#009900">'history'</FONT>]:
	    self._entryfield.configure(command=self._addHistory)

	<FONT COLOR="#DD0000"># Check keywords and initialise options.</FONT>
	self.initialiseoptions(ComboBox)

    <FONT COLOR="#DD0000">#======================================================================</FONT>

    <FONT COLOR="#DD0000"># Public methods</FONT>

<STRONG>    def get</STRONG>(self, first = None, last=None):
	if first is None:
	    return self._entryWidget.get()
	else:
	    return self._list.get(first, last)

<STRONG>    def invoke</STRONG>(self):
	if self[<FONT COLOR="#009900">'dropdown'</FONT>]:
	    self._postList()
	else:
	    self._selectCmd()

<STRONG>    def selectitem</STRONG>(self, index, setentry=1):
	if type(index) == types.StringType:
	    text = index
	    items = self._list.get(0, <FONT COLOR="#009900">'end'</FONT>)
	    if text in items:
		index = list(items).index(text)
	    else:
	    	raise IndexError, <FONT COLOR="#009900">'index "%s" not found'</FONT> % text
	elif setentry:
	    text = self._list.get(0, <FONT COLOR="#009900">'end'</FONT>)[index]

	self._list.select_clear(0, <FONT COLOR="#009900">'end'</FONT>)
	self._list.select_set(index, index)
	self._list.activate(index)
	self.see(index)
	if setentry:
	    self._entryfield.setentry(text)

    <FONT COLOR="#DD0000"># Need to explicitly forward this to override the stupid</FONT>
    <FONT COLOR="#DD0000"># (grid_)size method inherited from Tkinter.Frame.Grid.</FONT>
<STRONG>    def size</STRONG>(self):
	return self._list.size()

    <FONT COLOR="#DD0000">#======================================================================</FONT>

    <FONT COLOR="#DD0000"># Private methods for both dropdown and simple comboboxes.</FONT>

<STRONG>    def _addHistory</STRONG>(self):
	input = self._entryWidget.get()

	if input != <FONT COLOR="#009900">''</FONT>:
	    index = None
	    if self[<FONT COLOR="#009900">'unique'</FONT>]:
		<FONT COLOR="#DD0000"># If item is already in list, select it and return.</FONT>
		items = self._list.get(0, <FONT COLOR="#009900">'end'</FONT>)
		if input in items:
		    index = list(items).index(input)

	    if index is None:
		index = self._list.index(<FONT COLOR="#009900">'end'</FONT>)
		self._list.insert(<FONT COLOR="#009900">'end'</FONT>, input)

	    self.selectitem(index)
	    if self[<FONT COLOR="#009900">'autoclear'</FONT>]:
		self._entryWidget.delete(0, <FONT COLOR="#009900">'end'</FONT>)

	    <FONT COLOR="#DD0000"># Execute the selectioncommand on the new entry.</FONT>
	    self._selectCmd()

<STRONG>    def _next</STRONG>(self, event):
	size = self.size()
	if size <= 1:
	    return

	cursels = self.curselection()

	if len(cursels) == 0:
	    index = 0
	else:
	    index = string.atoi(cursels[0])
	    if index == size - 1:
		index = 0
	    else:
		index = index + 1

	self.selectitem(index)

<STRONG>    def _previous</STRONG>(self, event):
	size = self.size()
	if size <= 1:
	    return

	cursels = self.curselection()

	if len(cursels) == 0:
	    index = size - 1
	else:
	    index = string.atoi(cursels[0])
	    if index == 0:
		index = size - 1
	    else:
		index = index - 1

	self.selectitem(index)

<STRONG>    def _selectCmd</STRONG>(self, event=None):

	sels = self.getcurselection()
	if len(sels) == 0:
	    item = None
	else:
	    item = sels[0]
	    self._entryfield.setentry(item)

	cmd = self[<FONT COLOR="#009900">'selectioncommand'</FONT>]
	if callable(cmd):
	    cmd(item)

    <FONT COLOR="#DD0000">#======================================================================</FONT>

    <FONT COLOR="#DD0000"># Private method for simple combobox.</FONT>

<STRONG>    def _simpleBtnRelease</STRONG>(self, event):
	<FONT COLOR="#DD0000"># Only execute the command if the mouse was released over the</FONT>
	<FONT COLOR="#DD0000"># listbox.</FONT>
	if (event.x >= 0 and event.x < self.__listbox.winfo_width() and
		event.y >= 0 and event.y < self.__listbox.winfo_height()):
	    self._selectCmd()

    <FONT COLOR="#DD0000">#======================================================================</FONT>

    <FONT COLOR="#DD0000"># Private methods for dropdown combobox.</FONT>

<STRONG>    def _drawArrow</STRONG>(self, event=None, sunken=0):
	if sunken:
	    relief = <FONT COLOR="#009900">'sunken'</FONT>
	else:
	    relief = self[<FONT COLOR="#009900">'arrowrelief'</FONT>]
	flip = 0
	self._arrowBtn.configure(relief=relief)
	if not sunken and self._isPosted and self[<FONT COLOR="#009900">'fliparrow'</FONT>]:
		flip = 1

	fg = self[<FONT COLOR="#009900">'entry_foreground'</FONT>]

	self._arrowBtn.delete(<FONT COLOR="#009900">'arrow'</FONT>)

	bw = (string.atoi(self._arrowBtn[<FONT COLOR="#009900">'borderwidth'</FONT>]) + 
		string.atoi(self._arrowBtn[<FONT COLOR="#009900">'highlightthickness'</FONT>])) / 2
	h = string.atoi(self._arrowBtn[<FONT COLOR="#009900">'height'</FONT>]) + 2 * bw
	w = string.atoi(self._arrowBtn[<FONT COLOR="#009900">'width'</FONT>]) + 2 * bw

	if flip:
	     self._arrowBtn.create_polygon(0.25 * w + bw, 0.75 * h + bw,
					   0.75 * w + bw, 0.75 * h + bw,
					   0.5 * w + bw, 0.25 * h + bw - 1,
					   fill=fg, tag=<FONT COLOR="#009900">'arrow'</FONT>)
	else:
	     self._arrowBtn.create_polygon(0.25 * w + bw, 0.25 * h + bw,
					   0.75 * w + bw, 0.25 * h + bw,
					   0.5 * w + bw, 0.75 * h + bw,
					   fill=fg, tag=<FONT COLOR="#009900">'arrow'</FONT>)

<STRONG>    def _postList</STRONG>(self, event = None):
	if not self._isPosted:
	    self._isPosted = 1
	    self._drawArrow(sunken=1)

	    <FONT COLOR="#DD0000"># Make sure that the arrow is displayed sunken.</FONT>
	    self.update_idletasks()

	    x = self._entryfield.winfo_rootx()
	    y = self._entryfield.winfo_rooty() + \
		self._entryfield.winfo_height()
	    w = self._entryfield.winfo_width() + self._arrowBtn.winfo_width()
	    h =  self.__listbox.winfo_height()
	    sh = self.winfo_screenheight()

	    if y + h > sh and y > sh / 2:
		y = self._entryfield.winfo_rooty() - h

	    self._list.configure(hull_width=w)

	    <FONT COLOR="#DD0000"># To avoid flashes on X and to position the window</FONT>
	    <FONT COLOR="#DD0000"># correctly on Win95 (caused by Tk bugs):</FONT>
	    if os.name != "nt":
		self._popup.geometry(<FONT COLOR="#009900">'%+d%+d'</FONT> % (x, y))
	    self._popup.deiconify()

	    self._popup.wait_visibility()
	    self._popup.grab_set_global()
	    self._popup.tkraise()
	    self._popup.focus_set()

	    if os.name == "nt":
		self._popup.geometry(<FONT COLOR="#009900">'%+d%+d'</FONT> % (x, y))

	    self._drawArrow()

	    <FONT COLOR="#DD0000"># Ignore the first release of the mouse button after posting the</FONT>
	    <FONT COLOR="#DD0000"># dropdown list, unless the mouse enters the dropdown list.</FONT>
	    self._ignoreRelease = 1

<STRONG>    def _dropdownBtnRelease</STRONG>(self, event):
	if (event.widget == self._list.component(<FONT COLOR="#009900">'vertscrollbar'</FONT>) or
		event.widget == self._list.component(<FONT COLOR="#009900">'horizscrollbar'</FONT>)):
	    return

	if self._ignoreRelease:
	    self._unpostOnNextRelease()
	    return

	if (event.x >= 0 and event.x < self.__listbox.winfo_width() and
		event.y >= 0 and event.y < self.__listbox.winfo_height()):
	    self._selectCmd()

	self._unpostList()

<STRONG>    def _unpostOnNextRelease</STRONG>(self, event = None):
	self._ignoreRelease = 0

<STRONG>    def _resizeArrow</STRONG>(self, event):
	bw = (string.atoi(self._arrowBtn[<FONT COLOR="#009900">'borderwidth'</FONT>]) + 
		string.atoi(self._arrowBtn[<FONT COLOR="#009900">'highlightthickness'</FONT>]))
	newHeight = self._entryfield.winfo_reqheight() - 2 * bw
	newWidth = newHeight * self[<FONT COLOR="#009900">'buttonaspect'</FONT>]
	self._arrowBtn.configure(width=newWidth, height=newHeight)
	self._drawArrow()

<STRONG>    def _unpostList</STRONG>(self, event=None):
	self._popup.withdraw()
	self._popup.grab_release()

	self._isPosted = 0
	self._drawArrow()

<STRONG>    def _selectUnpost</STRONG>(self, event):
	self._selectCmd()
	self._unpostList()

Pmw.forwardmethods(ComboBox, Pmw.ScrolledListBox, <FONT COLOR="#009900">'_list'</FONT>)
Pmw.forwardmethods(ComboBox, Pmw.EntryField, <FONT COLOR="#009900">'_entryfield'</FONT>)

</PRE>

 </BODY> </HTML>