Specifies one of several styles for manipulating the selection.
The value of the option may be arbitrary, but the default bindings
expect it to be either single, browse, multiple,
or extended; the default value is browse.
Specifies the desired width for the window in characters.
If the font doesn't have a uniform width then the width of the
character ``0'' is used in translating from character units to
If zero or less, then the desired width for the window is made just
large enough to hold all the elements in the listbox.
The Listbox method creates a new window (given by the
$widget argument) and makes it into a listbox widget.
options, described above, may be specified on the command line
or in the option database
to configure aspects of the listbox such as its colors, font,
text, and relief. The listbox command returns its
$widget argument. At the time this command is invoked,
there must not exist a window named $widget, but
$widget's parent must exist.
A listbox is a widget that displays a list of strings, one per line.
When first created, a new listbox has no elements.
Elements may be added or deleted using methods described
below. In addition, one or more elements may be selected as described
If a listbox is exporting its selection (see exportSelection
option), then it will observe the standard X11 protocols
for handling the selection.
Listbox selections are available as type STRING;
the value of the selection will be the text of the selected elements, with
newlines separating the elements.
It is not necessary for all the elements to be
displayed in the listbox window at once; commands described below
may be used to change the view in the window. Listboxes allow
scrolling in both directions using the standard xScrollCommand
and yScrollCommand options.
They also support scanning, as described below.
The Listbox method creates a widget object.
This object supports the configure and cget methods
described in the Tk::options manpage which can be used to enquire and
modify the options described above.
The widget also inherits all the methods provided by the generic
The following additional methods are available for listbox widgets:
Sets the active element to the one indicated by index.
If index is outside the range of elements in the listbox
then the closest element is activated.
The active element is drawn with an underline when the widget
has the input focus, and its index may be retrieved with the
Returns a list of four numbers describing the bounding box of
the text in the element given by index.
The first two elements of the list give the x and y coordinates
of the upper-left corner of the screen area covered by the text
(specified in pixels relative to the widget) and the last two
elements give the width and height of the area, in pixels.
If no part of the element given by index is visible on the
or if index refers to a non-existent element,
then the result is an empty string; if the element is
partially visible, the result gives the full area of the element,
including any parts that are not visible.
Deletes one or more elements of the listbox. First and last
are indices specifying the first and last elements in the range
to delete. If last isn't specified it defaults to
first, i.e. a single element is deleted.
If last is omitted, returns the contents of the listbox
element indicated by first,
or an empty string if first refers to a non-existent element.
If last is specified, the command returns a list whose elements
are all of the listbox elements between first and last,
Both first and last may have any of the standard
forms for indices.
Records x and y and the current view in the listbox
window; used in conjunction with later scan dragto commands.
Typically this command is associated with a mouse button press in
the widget. It returns an empty string.
This command computes the difference between its x and y
arguments and the x and y arguments to the last
scan mark command for the widget.
It then adjusts the view by 10 times the
difference in coordinates. This command is typically associated
with mouse motion events in the widget, to produce the effect of
dragging the list at high speed through the window. The return
value is an empty string.
Adjust the view in the listbox so that the element given by index
If the element is already visible then the command has no effect;
if the element is near one edge of the window then the listbox
scrolls to bring the element into view at the edge; otherwise
the listbox scrolls to center the element.
Sets the selection anchor to the element given by index.
If index refers to a non-existent element, then the closest
element is used.
The selection anchor is the end of the selection that is fixed
while dragging out a selection with the mouse.
The index anchor may be used to refer to the anchor
This command is used to query and change the horizontal position of the
information in the widget's window. It can take any of the following
Returns a list containing two elements.
Each element is a real fraction between 0 and 1; together they describe
the horizontal span that is visible in the window.
For example, if the first element is .2 and the second element is .6,
20% of the listbox's text is off-screen to the left, the middle 40% is visible
in the window, and 40% of the text is off-screen to the right.
These are the same values passed to scrollbars via the -xscrollcommand
Adjusts the view in the window so that the character position given by
index is displayed at the left edge of the window.
Character positions are defined by the width of the character 0.
$listbox->xview(moveto => fraction)
Adjusts the view in the window so that fraction of the
total width of the listbox text is off-screen to the left.
fraction must be a fraction between 0 and 1.
$listbox->xview(scroll => number, what)
This command shifts the view in the window left or right according to
number and what.
Number must be an integer.
What must be either units or pages or an abbreviation
of one of these.
If what is units, the view adjusts left or right by
number character units (the width of the 0 character)
on the display; if it is pages then the view adjusts by
If number is negative then characters farther to the left
become visible; if it is positive then characters farther to the right
This command is used to query and change the vertical position of the
text in the widget's window.
It can take any of the following forms:
Returns a list containing two elements, both of which are real fractions
between 0 and 1.
The first element gives the position of the listbox element at the
top of the window, relative to the listbox as a whole (0.5 means
it is halfway through the listbox, for example).
The second element gives the position of the listbox element just after
the last one in the window, relative to the listbox as a whole.
These are the same values passed to scrollbars via the -yscrollcommand
Adjusts the view in the window so that the element given by
index is displayed at the top of the window.
$listbox->yview(moveto => fraction)
Adjusts the view in the window so that the element given by fraction
appears at the top of the window.
Fraction is a fraction between 0 and 1; 0 indicates the first
element in the listbox, 0.33 indicates the element one-third the
way through the listbox, and so on.
$listbox->yview(scroll => number, what)
This command adjusts the view in the window up or down according to
number and what.
Number must be an integer.
What must be either units or pages.
If what is units, the view adjusts up or down by
number lines; if it is pages then
the view adjusts by number screenfuls.
If number is negative then earlier elements
become visible; if it is positive then later elements
Tk automatically creates class bindings for listboxes that give them
Motif-like behavior. Much of the behavior of a listbox is determined
by its selectMode option, which selects one of four ways
of dealing with the selection.
If the selection mode is single or browse, at most one
element can be selected in the listbox at once.
In both modes, clicking button 1 on an element selects
it and deselects any other selected item.
In browse mode it is also possible to drag the selection
with button 1.
If the selection mode is multiple or extended,
any number of elements may be selected at once, including discontiguous
ranges. In multiple mode, clicking button 1 on an element
toggles its selection state without affecting any other elements.
In extended mode, pressing button 1 on an element selects
it, deselects everything else, and sets the anchor to the element
under the mouse; dragging the mouse with button 1
down extends the selection to include all the elements between
the anchor and the element under the mouse, inclusive.
Most people will probably want to use browse mode for
single selections and extended mode for multiple selections;
the other modes appear to be useful only in special situations.
In addition to the above behavior, the following additional behavior
is defined by the default bindings:
In extended mode, the selected range can be adjusted by pressing
button 1 with the Shift key down: this modifies the selection to
consist of the elements between the anchor and the element under
the mouse, inclusive.
The un-anchored end of this new selection can also be dragged with
the button down.
In extended mode, pressing button 1 with the Control key down
starts a toggle operation: the anchor is set to the element under
the mouse, and its selection state is reversed. The selection state
of other elements isn't changed.
If the mouse is dragged with button 1 down, then the selection state
of all elements between the anchor and the element under the mouse
is set to match that of the anchor element; the selection state of
all other elements remains what it was before the toggle operation
If the mouse leaves the listbox window with button 1 down, the window
scrolls away from the mouse, making information visible that used
to be off-screen on the side of the mouse.
The scrolling continues until the mouse re-enters the window, the
button is released, or the end of the listbox is reached.
If the Up or Down key is pressed, the location cursor (active
element) moves up or down one element.
If the selection mode is browse or extended then the
new active element is also selected and all other elements are
In extended mode the new active element becomes the
In extended mode, Shift-Up and Shift-Down move the location
cursor (active element) up or down one element and also extend
the selection to that element in a fashion similar to dragging
with mouse button 1.
The Left and Right keys scroll the listbox view left and right
by the width of the character 0.
Control-Left and Control-Right scroll the listbox view left and
right by the width of the window.
Control-Prior and Control-Next also scroll left and right by
the width of the window.