File: PmwScrolledListBox.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 (226 lines) | stat: -rw-r--r-- 10,165 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
<!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>PmwScrolledListBox.py</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<PRE>
<FONT COLOR="#DD0000"># Based on iwidgets2.2.0/scrolledlistbox.itk code.</FONT>

import types
import Tkinter
import Pmw

<STRONG><FONT COLOR="#CC6600">class ScrolledListBox</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">'dblclickcommand'</FONT>,    <FONT COLOR="#009900">''</FONT>,              None),
	    (<FONT COLOR="#009900">'hscrollmode'</FONT>,        <FONT COLOR="#009900">'dynamic'</FONT>,       self._hscrollMode),
	    (<FONT COLOR="#009900">'items'</FONT>,              (),              INITOPT),
	    (<FONT COLOR="#009900">'labelmargin'</FONT>,        0,               INITOPT),
	    (<FONT COLOR="#009900">'labelpos'</FONT>,           None,            INITOPT),
	    (<FONT COLOR="#009900">'scrollmargin'</FONT>,       2,               INITOPT),
	    (<FONT COLOR="#009900">'selectioncommand'</FONT>,   <FONT COLOR="#009900">''</FONT>,              None),
	    (<FONT COLOR="#009900">'vscrollmode'</FONT>,        <FONT COLOR="#009900">'dynamic'</FONT>,       self._vscrollMode),
	)
	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()

	<FONT COLOR="#DD0000"># Create the listbox widget.</FONT>
	self._listbox = self.createcomponent(<FONT COLOR="#009900">'listbox'</FONT>,
		(), None,
		Tkinter.Listbox, (interior,),
		xscrollcommand=self._scrollListX,
		yscrollcommand=self._scrollListY)
	self._listbox.grid(row = 2, column = 2, sticky = <FONT COLOR="#009900">'news'</FONT>)
	interior.grid_rowconfigure(2, weight = 1, minsize = 0)
	interior.grid_columnconfigure(2, weight = 1, minsize = 0)

	<FONT COLOR="#DD0000"># Create the vertical scrollbar</FONT>
	self._vertScrollbar = self.createcomponent(<FONT COLOR="#009900">'vertscrollbar'</FONT>,
		(), <FONT COLOR="#009900">'Scrollbar'</FONT>,
		Tkinter.Scrollbar, (interior,),
		orient=<FONT COLOR="#009900">'vertical'</FONT>, command=self._listbox.yview)

	<FONT COLOR="#DD0000"># Create the horizontal scrollbar</FONT>
	self._horizScrollbar = self.createcomponent(<FONT COLOR="#009900">'horizscrollbar'</FONT>,
		(), <FONT COLOR="#009900">'Scrollbar'</FONT>,
		Tkinter.Scrollbar, (interior,),
	        orient=<FONT COLOR="#009900">'horizontal'</FONT>, command=self._listbox.xview)

	self.createlabel(interior, childCols = 3, childRows = 3)

	<FONT COLOR="#DD0000"># Add the items specified by the initialisation option.</FONT>
	items = self[<FONT COLOR="#009900">'items'</FONT>]
	if type(items) != types.TupleType:
	    items = tuple(items)
	if len(items) > 0:
	    apply(self._listbox.insert, (<FONT COLOR="#009900">'end'</FONT>,) + items)

	tag = <FONT COLOR="#009900">'SLBSelect'</FONT> + str(self)
	self.bind_class(tag, <FONT COLOR="#009900">'&lt;Control-Key-backslash&gt;'</FONT>, self._makeSelection)
	self.bind_class(tag, <FONT COLOR="#009900">'&lt;Control-Key-slash&gt;'</FONT>, self._makeSelection)
	self.bind_class(tag, <FONT COLOR="#009900">'&lt;Key-Escape&gt;'</FONT>, self._makeSelection)
	self.bind_class(tag, <FONT COLOR="#009900">'&lt;Shift-Key-Select&gt;'</FONT>, self._makeSelection)
	self.bind_class(tag, <FONT COLOR="#009900">'&lt;Control-Shift-Key-space&gt;'</FONT>, self._makeSelection)
	self.bind_class(tag, <FONT COLOR="#009900">'&lt;Key-Select&gt;'</FONT>, self._makeSelection)
	self.bind_class(tag, <FONT COLOR="#009900">'&lt;Key-space&gt;'</FONT>, self._makeSelection)
	self.bind_class(tag, <FONT COLOR="#009900">'&lt;Control-Shift-Key-End&gt;'</FONT>, self._makeSelection)
	self.bind_class(tag, <FONT COLOR="#009900">'&lt;Control-Key-End&gt;'</FONT>, self._makeSelection)
	self.bind_class(tag, <FONT COLOR="#009900">'&lt;Control-Shift-Key-Home&gt;'</FONT>, self._makeSelection)
	self.bind_class(tag, <FONT COLOR="#009900">'&lt;Control-Key-Home&gt;'</FONT>, self._makeSelection)
	self.bind_class(tag, <FONT COLOR="#009900">'&lt;Shift-Key-Down&gt;'</FONT>, self._makeSelection)
	self.bind_class(tag, <FONT COLOR="#009900">'&lt;Shift-Key-Up&gt;'</FONT>, self._makeSelection)
	self.bind_class(tag, <FONT COLOR="#009900">'&lt;Control-Button-1&gt;'</FONT>, self._makeSelection)
	self.bind_class(tag, <FONT COLOR="#009900">'&lt;Shift-Button-1&gt;'</FONT>, self._makeSelection)
	self.bind_class(tag, <FONT COLOR="#009900">'&lt;ButtonRelease-1&gt;'</FONT>, self._makeSelection)
	self.bind_class(tag, <FONT COLOR="#009900">'&lt;Double-1&gt;'</FONT>, self._doubleClick)

	self._listbox.bindtags(self._listbox.bindtags() + (tag,))

	<FONT COLOR="#DD0000"># Initialise instance variables.</FONT>
	self._vertMode = None
	self._horizMode = None

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

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

    <FONT COLOR="#DD0000"># Configuration methods.</FONT>

<STRONG>    def _vscrollMode</STRONG>(self):
	mode = self[<FONT COLOR="#009900">'vscrollmode'</FONT>]

	if mode == <FONT COLOR="#009900">'static'</FONT>:
	    self._vertScrollbarDisplay(1)
	elif mode == <FONT COLOR="#009900">'dynamic'</FONT> or mode == <FONT COLOR="#009900">'none'</FONT>:
	    self._vertScrollbarDisplay(0)
	else:
	    message = <FONT COLOR="#009900">'bad vscrollmode option "%s": should be static, dynamic, or none'</FONT> % mode
	    raise ValueError, message

<STRONG>    def _hscrollMode</STRONG>(self):
	mode = self[<FONT COLOR="#009900">'hscrollmode'</FONT>]

	if mode == <FONT COLOR="#009900">'static'</FONT>:
	    self._horizScrollbarDisplay(1)
	elif mode == <FONT COLOR="#009900">'dynamic'</FONT> or mode == <FONT COLOR="#009900">'none'</FONT>:
	    self._horizScrollbarDisplay(0)
	else:
	    message = <FONT COLOR="#009900">'bad hscrollmode option "%s": should be static, dynamic, or none'</FONT> % mode
	    raise ValueError, message

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

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

<STRONG>    def getcurselection</STRONG>(self):
	rtn = []
	for sel in self.curselection():
	    rtn.append(self._listbox.get(sel))
	return tuple(rtn)

<STRONG>    def justify</STRONG>(self, direction):
	if direction == <FONT COLOR="#009900">'left'</FONT>:
	    self._listbox.xview(<FONT COLOR="#009900">'moveto'</FONT>, 0)
	elif direction == <FONT COLOR="#009900">'right'</FONT>:
	    self._listbox.xview(<FONT COLOR="#009900">'moveto'</FONT>, 1)
	elif direction == <FONT COLOR="#009900">'top'</FONT>:
	    self._listbox.yview(<FONT COLOR="#009900">'moveto'</FONT>, 0)
	elif direction == <FONT COLOR="#009900">'bottom'</FONT>:
	    self._listbox.yview(<FONT COLOR="#009900">'moveto'</FONT>, 1)
	else:
	    validValues = <FONT COLOR="#009900">'left, right, top, or bottom'</FONT>
	    raise ValueError, \
		<FONT COLOR="#009900">'bad justify argument "%s": should be %s'</FONT> \
		    % (direction, validValues)

<STRONG>    def setlist</STRONG>(self, items):
        self._listbox.delete(0, <FONT COLOR="#009900">'end'</FONT>)
	if len(items) > 0:
	    if type(items) != types.TupleType:
		items = tuple(items)
	    apply(self._listbox.insert, (0,) + items)

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

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

<STRONG>    def _makeSelection</STRONG>(self, event):
	command = self[<FONT COLOR="#009900">'selectioncommand'</FONT>]
	if callable(command):
	    command()

<STRONG>    def _doubleClick</STRONG>(self, event):
	command = self[<FONT COLOR="#009900">'dblclickcommand'</FONT>]
	if callable(command):
	    command()

<STRONG>    def _vertScrollbarDisplay</STRONG>(self, mode):
	if mode != self._vertMode:
	    self._vertMode = mode

	    margin = self[<FONT COLOR="#009900">'scrollmargin'</FONT>]
	    interior = self.interior()

	    if self._vertMode:
		self._vertScrollbar.grid(row = 2, column = 4, sticky = <FONT COLOR="#009900">'news'</FONT>)
		interior.grid_columnconfigure(3, minsize = margin)
	    else:
		self._vertScrollbar.grid_forget()
		interior.grid_columnconfigure(3, minsize = 0)

<STRONG>    def _horizScrollbarDisplay</STRONG>(self, mode):
	if mode != self._horizMode:
	    self._horizMode = mode

	    margin = self[<FONT COLOR="#009900">'scrollmargin'</FONT>]
	    interior = self.interior()

	    if self._horizMode:
		self._horizScrollbar.grid(row = 4, column = 2, sticky = <FONT COLOR="#009900">'news'</FONT>)
		interior.grid_rowconfigure(3, minsize = margin)
	    else:
		self._horizScrollbar.grid_forget()
		interior.grid_rowconfigure(3, minsize = 0)

<STRONG>    def _scrollListX</STRONG>(self, first, last):
	self._horizScrollbar.set(first, last)

	if self[<FONT COLOR="#009900">'hscrollmode'</FONT>] == <FONT COLOR="#009900">'dynamic'</FONT>:
	    if first == <FONT COLOR="#009900">'0'</FONT> and last == <FONT COLOR="#009900">'1'</FONT>:
		self._horizScrollbarDisplay(0)
	    else:
		self._horizScrollbarDisplay(1)

<STRONG>    def _scrollListY</STRONG>(self, first, last):
	self._vertScrollbar.set(first, last)

	if self[<FONT COLOR="#009900">'vscrollmode'</FONT>] == <FONT COLOR="#009900">'dynamic'</FONT>:
	    if first == <FONT COLOR="#009900">'0'</FONT> and last == <FONT COLOR="#009900">'1'</FONT>:
		self._vertScrollbarDisplay(0)
	    else:
		self._vertScrollbarDisplay(1)

    <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._listbox.size()

Pmw.forwardmethods(ScrolledListBox, Tkinter.Listbox, <FONT COLOR="#009900">'_listbox'</FONT>)

</PRE>

 </BODY> </HTML>