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