The purpose of a listbox widget is to display a set of
lines of text. Generally they are intended to allow the
user to select one or more items from a list. If you need
something more like a text editor, see Section 17, “The Text
widget”.
To create a new listbox widget inside a root window or
frame
:master
w
= Listbox (master
,option
, ... )
This constructor returns the new listbox widget. Options:
bg or
background | The background color in the listbox. |
bd or
borderwidth | The width of the border around the listbox. Default is 2. For possible values, see Section 4.1, “Dimensions”. |
cursor | The cursor that appears when the mouse is over the listbox. See Section 4.8, “Cursors”. |
font | The font used for the text in the listbox. See Section 4.4, “Type fonts”. |
fg or
foreground | The color used for the text in the listbox. See Section 4.3, “Colors”. |
height | Number of lines (not pixels!) shown in the listbox. Default is 10. |
highlightbackground | Color of the focus highlight when the widget does not have focus. See Section 23, “Focus: routing keyboard input”. |
highlightcolor | Color shown in the focus highlight when the widget has the focus. |
highlightthickness | Thickness of the focus highlight. |
relief | Selects three-dimensional border shading effects.
The default is SUNKEN . For
other values, see Section 4.6, “Relief styles”. |
selectbackground | The background color to use displaying selected text. |
selectborderwidth | The width of the border to use around selected
text. The default is that the selected item is
shown in a solid block of color
selectbackground ; if you
increase the
selectborderwidth , the
entries are moved farther apart and the selected
entry shows RAISED relief
(see Section 4.6, “Relief styles”). |
selectforeground | The foreground color to use displaying selected text. |
selectmode | Determines how many items can be
selected, and how mouse drags affect the
selection:
|
takefocus | Normally, the focus will tab through listbox widgets. Set this option to 0 to take the widget out of the sequence. See Section 23, “Focus: routing keyboard input”. |
width | The width of the widget in characters (not pixels!). The width is based on an average character, so some strings of this length in proportional fonts may not fit. The default is 20. |
xscrollcommand | If you want to allow the user to scroll the
listbox horizontally, you can link your listbox
widget to a horizontal scrollbar. Set this option
to the .set method of the
scrollbar. See Section 11.1, “Scrolling a Listbox widget” for more on scrollable
listbox widgets. |
yscrollcommand | If you want to allow the user to scroll the
listbox vertically, you can link your listbox
widget to a vertical scrollbar. Set this option to
the .set method of the
scrollbar. See Section 11.1, “Scrolling a Listbox widget”. |
A special set of index forms is used for many of the methods on listbox objects:
If you specify an index as an integer, it refers to the line in the listbox with that index, counting from 0.
Index END
refers to the last
line in the listbox.
Index ACTIVE
refers to the
selected line. If the listbox allows multiple
selections, it refers to the line that was last
selected.
An index string of the form
"@
refers to the line
closest to coordinate
(x
,y
"
,x
)
relative to the widget's upper left
corner.y
Methods on listbox objects include:
.activate (
index
)
Selects the line specifies by the given
.index
.curselection()
Returns a tuple containing the line numbers of the selected element or elements, counting from 0. If nothing is selected, returns an empty tuple.
.delete ( first
, last
=None )
Deletes the lines whose indices are in the range
[
,
first
],
inclusive
(contrary to the usual Python idiom, where deletion
stops short of the last index), counting from 0. If
the second argument is omitted, the single line with
index
last
is deleted.first
.get ( first
, last
=None )
Returns a tuple containing the text of the lines
with indices from
to
first
,
inclusive. If the second argument is omitted,
returns the text of the line closest to
last
.first
.index ( i
)
If possible, positions the visible part of the
listbox so that the line containing index
is at the top of the widget.i
.insert ( index
, *elements
)
Insert one or more new lines into the listbox
before the line specified by
.
Use index
END
as the first argument
if you want to add new lines to the end of the
listbox.
.nearest ( y
)
Return the index of the visible line closest to
the y-coordinate y
relative to the listbox widget.
.see ( index
)
Adjust the position of the listbox so that the
line referred to by
is visible.index
.selection_clear ( first
, last
=None )
Unselects all of the lines between indices
and
first
,
inclusive. If the second argument is omitted,
unselects the line with index
last
.first
.selection_includes ( index
)
Returns 1 if the line with the given
is selected, else returns 0.index
.selection_set ( first
, last
=None )
Selects all of the lines between indices
and
first
,
inclusive. If the second argument is omitted,
selects the line with index
last
.first
.size()
Returns the number of lines in the listbox.
.xview()
To make the listbox horizontally scrollable, set
the command
option of the
associated horizontal scrollbar to this method. See
Section 11.1, “Scrolling a Listbox
widget”.
.xview_moveto ( fraction
)
Scroll the listbox so that the leftmost
of the width of its longest line is outside the left
side of the listbox. Fraction is in the range
[0,1].fraction
.xview_scroll ( number
, what
)
Scrolls the listbox horizontally. For the
argument, use either what
UNITS
to
scroll by characters, or PAGES
to scroll by pages, that is, by the width of the
listbox. The
argument tells how many to scroll; negative values
move the text to the right within the listbox,
positive values leftward.number
.yview()
To make the listbox vertically scrollable, set
the
option of the associated vertical scrollbar to this
method. See Section 11.1, “Scrolling a command
Listbox
widget”.
.yview_moveto ( fraction
)
Scroll the listbox so that the top
of the width of its longest line is outside the left
side of the listbox. Fraction is in the range
[0,1].fraction
.yview_scroll ( number
, what
)
Scrolls the listbox vertically. For the
argument, use either what
UNITS
to
scroll by lines, or PAGES
to
scroll by pages, that is, by the height of the
listbox. The
argument tells how many to scroll; negative values
move the text downward inside the listbox, and
positive values move the text up.number