1listbox(n) Tk Built-In Commands listbox(n)
2
3
4
5______________________________________________________________________________
6
8 listbox - Create and manipulate listbox widgets
9
11 listbox pathName ?options?
12
14 -activestyle -height -selectforeground
15 -background -highlightbackground -setgrid
16 -borderwidth -highlightcolor -state
17 -cursor -highlightthickness -takefocus
18 -disabledforeground -relief -width
19 -exportselection -selectbackground -xscrollcommand
20 -font -selectborderwidth -yscrollcommand
21 -foreground
22
23 See the options manual entry for details on the standard options.
24
26 [-activestyle activeStyle] Specifies the style in which to draw the │
27 active element. This must be one of dotbox (show a focus ring around │
28 the active element), none (no special indication of active element) or │
29 underline (underline the active element). The default is underline. │
30 [-height height] Specifies the desired height for the window, in lines. │
31 If zero or less, then the desired height for the window is made just │
32 large enough to hold all the elements in the listbox. [-listvari‐ │
33 able listVariable] Specifies the name of a variable. The value of the │
34 variable is a list to be displayed inside the widget; if the variable │
35 value changes then the widget will automatically update itself to │
36 reflect the new value. Attempts to assign a variable with an invalid │
37 list value to -listvariable will cause an error. Attempts to unset a │
38 variable in use as a -listvariable will fail but will not generate an │
39 error. [-selectmode selectMode] Specifies one of several styles for │
40 manipulating the selection. The value of the option may be arbitrary, │
41 but the default bindings expect it to be either single, browse, multi‐ │
42 ple, or extended; the default value is browse. [-state state] Speci‐ │
43 fies one of two states for the listbox: normal or disabled. If the │
44 listbox is disabled then items may not be inserted or deleted, items │
45 are drawn in the -disabledforeground color, and selection cannot be │
46 modified and is not shown (though selection information is retained). │
47 [-width width] Specifies the desired width for the window in charac‐ │
48 ters. If the font doesn't have a uniform width then the width of the │
49 character ``0'' is used in translating from character units to screen │
50 units. If zero or less, then the desired width for the window is made │
51 just large enough to hold all the elements in the listbox. │
52_________________________________________________________________ │
53
54
56 The listbox command creates a new window (given by the pathName argu‐ │
57 ment) and makes it into a listbox widget. Additional options, │
58 described above, may be specified on the command line or in the option │
59 database to configure aspects of the listbox such as its colors, font, │
60 text, and relief. The listbox command returns its pathName argument. │
61 At the time this command is invoked, there must not exist a window │
62 named pathName, but pathName's parent must exist. │
63
64 A listbox is a widget that displays a list of strings, one per line. │
65 When first created, a new listbox has no elements. Elements may be │
66 added or deleted using widget commands described below. In addition, │
67 one or more elements may be selected as described below. If a listbox │
68 is exporting its selection (see exportSelection option), then it will │
69 observe the standard X11 protocols for handling the selection. Listbox │
70 selections are available as type STRING; the value of the selection │
71 will be the text of the selected elements, with newlines separating the │
72 elements. │
73
74 It is not necessary for all the elements to be displayed in the listbox │
75 window at once; commands described below may be used to change the │
76 view in the window. Listboxes allow scrolling in both directions using │
77 the standard xScrollCommand and yScrollCommand options. They also sup‐ │
78 port scanning, as described below. │
79
80
82 Many of the widget commands for listboxes take one or more indices as │
83 arguments. An index specifies a particular element of the listbox, in │
84 any of the following ways: │
85
86 number │
87 Specifies the element as a numerical index, where 0 corre‐ │
88 sponds to the first element in the listbox. │
89
90 active │
91 Indicates the element that has the location cursor. This │
92 element will be displayed as specified by -activestyle when │
93 the listbox has the keyboard focus, and it is specified │
94 with the activate widget command. │
95
96 anchor │
97 Indicates the anchor point for the selection, which is set │
98 with the selection anchor widget command. │
99
100 end │
101 Indicates the end of the listbox. For most commands this │
102 refers to the last element in the listbox, but for a few │
103 commands such as index and insert it refers to the element │
104 just after the last one.
105
106 @x,y Indicates the element that covers the point in the listbox
107 window specified by x and y (in pixel coordinates). If no
108 element covers that point, then the closest element to that
109 point is used.
110
111 In the widget command descriptions below, arguments named index, first,
112 and last always contain text indices in one of the above forms.
113
114
116 The listbox command creates a new Tcl command whose name is pathName.
117 This command may be used to invoke various operations on the widget.
118 It has the following general form:
119 pathName option ?arg arg ...?
120 Option and the args determine the exact behavior of the command. The
121 following commands are possible for listbox widgets:
122
123 pathName activate index
124 Sets the active element to the one indicated by index. If index │
125 is outside the range of elements in the listbox then the closest │
126 element is activated. The active element is drawn as specified
127 by -activestyle when the widget has the input focus, and its
128 index may be retrieved with the index active.
129
130 pathName bbox index
131 Returns a list of four numbers describing the bounding box of
132 the text in the element given by index. The first two elements
133 of the list give the x and y coordinates of the upper-left cor‐
134 ner of the screen area covered by the text (specified in pixels
135 relative to the widget) and the last two elements give the width
136 and height of the area, in pixels. If no part of the element
137 given by index is visible on the screen, or if index refers to a │
138 non-existent element, then the result is an empty string; if
139 the element is partially visible, the result gives the full area
140 of the element, including any parts that are not visible.
141
142 pathName cget option
143 Returns the current value of the configuration option given by
144 option. Option may have any of the values accepted by the list‐
145 box command.
146
147 pathName configure ?option? ?value option value ...?
148 Query or modify the configuration options of the widget. If no
149 option is specified, returns a list describing all of the avail‐
150 able options for pathName (see Tk_ConfigureInfo for information
151 on the format of this list). If option is specified with no
152 value, then the command returns a list describing the one named
153 option (this list will be identical to the corresponding sublist
154 of the value returned if no option is specified). If one or
155 more option-value pairs are specified, then the command modifies
156 the given widget option(s) to have the given value(s); in this
157 case the command returns an empty string. Option may have any
158 of the values accepted by the listbox command.
159
160 pathName curselection
161 Returns a list containing the numerical indices of all of the
162 elements in the listbox that are currently selected. If there
163 are no elements selected in the listbox then an empty string is
164 returned.
165
166 pathName delete first ?last?
167 Deletes one or more elements of the listbox. First and last are
168 indices specifying the first and last elements in the range to
169 delete. If last isn't specified it defaults to first, i.e. a
170 single element is deleted.
171
172 pathName get first ?last?
173 If last is omitted, returns the contents of the listbox element
174 indicated by first, or an empty string if first refers to a non- │
175 existent element. If last is specified, the command returns a
176 list whose elements are all of the listbox elements between
177 first and last, inclusive. Both first and last may have any of
178 the standard forms for indices.
179
180 pathName index index
181 Returns the integer index value that corresponds to index. If │
182 index is end the return value is a count of the number of ele‐ │
183 ments in the listbox (not the index of the last element).
184
185 pathName insert index ?element element ...?
186 Inserts zero or more new elements in the list just before the
187 element given by index. If index is specified as end then the
188 new elements are added to the end of the list. Returns an empty
189 string.
190
191 pathName itemcget index option
192 Returns the current value of the item configuration option given
193 by option. Option may have any of the values accepted by the
194 listbox itemconfigure command.
195
196 pathName itemconfigure index ?option? ?value? ?option value ...?
197 Query or modify the configuration options of an item in the
198 listbox. If no option is specified, returns a list describing
199 all of the available options for the item (see Tk_ConfigureInfo
200 for information on the format of this list). If option is spec‐
201 ified with no value, then the command returns a list describing
202 the one named option (this list will be identical to the corre‐
203 sponding sublist of the value returned if no option is speci‐
204 fied). If one or more option-value pairs are specified, then
205 the command modifies the given widget option(s) to have the
206 given value(s); in this case the command returns an empty
207 string. The following options are currently supported for items:
208
209 -background color
210 Color specifies the background color to use when display‐
211 ing the item. It may have any of the forms accepted by
212 Tk_GetColor.
213
214 -foreground color
215 Color specifies the foreground color to use when display‐
216 ing the item. It may have any of the forms accepted by
217 Tk_GetColor.
218
219 -selectbackground color
220 color specifies the background color to use when display‐
221 ing the item while it is selected. It may have any of the
222 forms accepted by Tk_GetColor.
223
224 -selectforeground color
225 color specifies the foreground color to use when display‐
226 ing the item while it is selected. It may have any of the
227 forms accepted by Tk_GetColor.
228
229 pathName nearest y
230 Given a y-coordinate within the listbox window, this command
231 returns the index of the (visible) listbox element nearest to
232 that y-coordinate.
233
234 pathName scan option args
235 This command is used to implement scanning on listboxes. It has
236 two forms, depending on option:
237
238 pathName scan mark x y
239 Records x and y and the current view in the listbox win‐
240 dow; used in conjunction with later scan dragto com‐
241 mands. Typically this command is associated with a mouse
242 button press in the widget. It returns an empty string.
243
244 pathName scan dragto x y.
245 This command computes the difference between its x and y
246 arguments and the x and y arguments to the last scan mark
247 command for the widget. It then adjusts the view by 10
248 times the difference in coordinates. This command is
249 typically associated with mouse motion events in the wid‐
250 get, to produce the effect of dragging the list at high
251 speed through the window. The return value is an empty
252 string.
253
254 pathName see index
255 Adjust the view in the listbox so that the element given by
256 index is visible. If the element is already visible then the
257 command has no effect; if the element is near one edge of the
258 window then the listbox scrolls to bring the element into view
259 at the edge; otherwise the listbox scrolls to center the ele‐
260 ment.
261
262 pathName selection option arg
263 This command is used to adjust the selection within a listbox.
264 It has several forms, depending on option:
265
266 pathName selection anchor index
267 Sets the selection anchor to the element given by index. │
268 If index refers to a non-existent element, then the clos‐ │
269 est element is used. The selection anchor is the end of
270 the selection that is fixed while dragging out a selec‐
271 tion with the mouse. The index anchor may be used to
272 refer to the anchor element.
273
274 pathName selection clear first ?last?
275 If any of the elements between first and last (inclusive)
276 are selected, they are deselected. The selection state
277 is not changed for elements outside this range.
278
279 pathName selection includes index
280 Returns 1 if the element indicated by index is currently
281 selected, 0 if it isn't.
282
283 pathName selection set first ?last?
284 Selects all of the elements in the range between first
285 and last, inclusive, without affecting the selection
286 state of elements outside that range.
287
288 pathName size
289 Returns a decimal string indicating the total number of elements
290 in the listbox.
291
292 pathName xview args
293 This command is used to query and change the horizontal position
294 of the information in the widget's window. It can take any of
295 the following forms:
296
297 pathName xview
298 Returns a list containing two elements. Each element is
299 a real fraction between 0 and 1; together they describe
300 the horizontal span that is visible in the window. For
301 example, if the first element is .2 and the second ele‐
302 ment is .6, 20% of the listbox's text is off-screen to
303 the left, the middle 40% is visible in the window, and
304 40% of the text is off-screen to the right. These are
305 the same values passed to scrollbars via the -xscrollcom‐
306 mand option.
307
308 pathName xview index
309 Adjusts the view in the window so that the character
310 position given by index is displayed at the left edge of
311 the window. Character positions are defined by the width
312 of the character 0.
313
314 pathName xview moveto fraction
315 Adjusts the view in the window so that fraction of the
316 total width of the listbox text is off-screen to the
317 left. fraction must be a fraction between 0 and 1.
318
319 pathName xview scroll number what
320 This command shifts the view in the window left or right
321 according to number and what. Number must be an integer.
322 What must be either units or pages or an abbreviation of
323 one of these. If what is units, the view adjusts left or
324 right by number character units (the width of the 0 char‐
325 acter) on the display; if it is pages then the view
326 adjusts by number screenfuls. If number is negative then
327 characters farther to the left become visible; if it is
328 positive then characters farther to the right become vis‐
329 ible.
330
331 pathName yview ?args?
332 This command is used to query and change the vertical position
333 of the text in the widget's window. It can take any of the fol‐
334 lowing forms:
335
336 pathName yview
337 Returns a list containing two elements, both of which are
338 real fractions between 0 and 1. The first element gives
339 the position of the listbox element at the top of the
340 window, relative to the listbox as a whole (0.5 means it
341 is halfway through the listbox, for example). The second
342 element gives the position of the listbox element just
343 after the last one in the window, relative to the listbox
344 as a whole. These are the same values passed to scroll‐
345 bars via the -yscrollcommand option.
346
347 pathName yview index
348 Adjusts the view in the window so that the element given
349 by index is displayed at the top of the window.
350
351 pathName yview moveto fraction
352 Adjusts the view in the window so that the element given
353 by fraction appears at the top of the window. Fraction
354 is a fraction between 0 and 1; 0 indicates the first
355 element in the listbox, 0.33 indicates the element one-
356 third the way through the listbox, and so on.
357
358 pathName yview scroll number what
359 This command adjusts the view in the window up or down
360 according to number and what. Number must be an integer.
361 What must be either units or pages. If what is units,
362 the view adjusts up or down by number lines; if it is
363 pages then the view adjusts by number screenfuls. If
364 number is negative then earlier elements become visible;
365 if it is positive then later elements become visible.
366
367
369 Tk automatically creates class bindings for listboxes that give them
370 Motif-like behavior. Much of the behavior of a listbox is determined
371 by its selectMode option, which selects one of four ways of dealing
372 with the selection.
373
374 If the selection mode is single or browse, at most one element can be
375 selected in the listbox at once. In both modes, clicking button 1 on
376 an element selects it and deselects any other selected item. In browse
377 mode it is also possible to drag the selection with button 1.
378
379 If the selection mode is multiple or extended, any number of elements
380 may be selected at once, including discontiguous ranges. In multiple
381 mode, clicking button 1 on an element toggles its selection state with‐
382 out affecting any other elements. In extended mode, pressing button 1
383 on an element selects it, deselects everything else, and sets the
384 anchor to the element under the mouse; dragging the mouse with button
385 1 down extends the selection to include all the elements between the
386 anchor and the element under the mouse, inclusive.
387
388 Most people will probably want to use browse mode for single selections
389 and extended mode for multiple selections; the other modes appear to be
390 useful only in special situations.
391
392 Any time the selection changes in the listbox, the virtual event
393 <<ListboxSelect>> will be generated. It is easiest to bind to this
394 event to be made aware of any changes to listbox selection.
395
396 In addition to the above behavior, the following additional behavior is
397 defined by the default bindings:
398
399 [1] In extended mode, the selected range can be adjusted by pressing
400 button 1 with the Shift key down: this modifies the selection
401 to consist of the elements between the anchor and the element
402 under the mouse, inclusive. The un-anchored end of this new
403 selection can also be dragged with the button down.
404
405 [2] In extended mode, pressing button 1 with the Control key down
406 starts a toggle operation: the anchor is set to the element
407 under the mouse, and its selection state is reversed. The
408 selection state of other elements isn't changed. If the mouse
409 is dragged with button 1 down, then the selection state of all
410 elements between the anchor and the element under the mouse is
411 set to match that of the anchor element; the selection state of
412 all other elements remains what it was before the toggle opera‐
413 tion began.
414
415 [3] If the mouse leaves the listbox window with button 1 down, the
416 window scrolls away from the mouse, making information visible
417 that used to be off-screen on the side of the mouse. The
418 scrolling continues until the mouse re-enters the window, the
419 button is released, or the end of the listbox is reached.
420
421 [4] Mouse button 2 may be used for scanning. If it is pressed and
422 dragged over the listbox, the contents of the listbox drag at
423 high speed in the direction the mouse moves.
424
425 [5] If the Up or Down key is pressed, the location cursor (active
426 element) moves up or down one element. If the selection mode is
427 browse or extended then the new active element is also selected
428 and all other elements are deselected. In extended mode the new
429 active element becomes the selection anchor.
430
431 [6] In extended mode, Shift-Up and Shift-Down move the location cur‐
432 sor (active element) up or down one element and also extend the
433 selection to that element in a fashion similar to dragging with
434 mouse button 1.
435
436 [7] The Left and Right keys scroll the listbox view left and right
437 by the width of the character 0. Control-Left and Control-Right
438 scroll the listbox view left and right by the width of the win‐
439 dow. Control-Prior and Control-Next also scroll left and right
440 by the width of the window.
441
442 [8] The Prior and Next keys scroll the listbox view up and down by
443 one page (the height of the window).
444
445 [9] The Home and End keys scroll the listbox horizontally to the
446 left and right edges, respectively.
447
448 [10] Control-Home sets the location cursor to the first element in
449 the listbox, selects that element, and deselects everything else
450 in the listbox.
451
452 [11] Control-End sets the location cursor to the last element in the
453 listbox, selects that element, and deselects everything else in
454 the listbox.
455
456 [12] In extended mode, Control-Shift-Home extends the selection to
457 the first element in the listbox and Control-Shift-End extends
458 the selection to the last element.
459
460 [13] In multiple mode, Control-Shift-Home moves the location cursor
461 to the first element in the listbox and Control-Shift-End moves
462 the location cursor to the last element.
463
464 [14] The space and Select keys make a selection at the location cur‐
465 sor (active element) just as if mouse button 1 had been pressed
466 over this element.
467
468 [15] In extended mode, Control-Shift-space and Shift-Select extend
469 the selection to the active element just as if button 1 had been
470 pressed with the Shift key down.
471
472 [16] In extended mode, the Escape key cancels the most recent selec‐
473 tion and restores all the elements in the selected range to
474 their previous selection state.
475
476 [17] Control-slash selects everything in the widget, except in single
477 and browse modes, in which case it selects the active element
478 and deselects everything else.
479
480 [18] Control-backslash deselects everything in the widget, except in
481 browse mode where it has no effect.
482
483 [19] The F16 key (labelled Copy on many Sun workstations) or Meta-w
484 copies the selection in the widget to the clipboard, if there is
485 a selection.
486
487
488 The behavior of listboxes can be changed by defining new bindings for
489 individual widgets or by redefining the class bindings.
490
491
493 listbox, widget
494
495
496
497Tk 8.4 listbox(n)