Next / Previous / Contents / TCC Help System / NM Tech homepage

11. The Listbox widget

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 backgroundThe background color in the listbox.
bd or borderwidthThe width of the border around the listbox. Default is 2. For possible values, see Section 4.1, “Dimensions”.
cursorThe cursor that appears when the mouse is over the listbox. See Section 4.8, “Cursors”.
fontThe font used for the text in the listbox. See Section 4.4, “Type fonts”.
fg or foregroundThe color used for the text in the listbox. See Section 4.3, “Colors”.
heightNumber of lines (not pixels!) shown in the listbox. Default is 10.
highlightbackgroundColor of the focus highlight when the widget does not have focus. See Section 23, “Focus: routing keyboard input”.
highlightcolorColor shown in the focus highlight when the widget has the focus.
highlightthicknessThickness of the focus highlight.
reliefSelects three-dimensional border shading effects. The default is SUNKEN. For other values, see Section 4.6, “Relief styles”.
selectbackgroundThe background color to use displaying selected text.
selectborderwidthThe 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”).
selectforegroundThe foreground color to use displaying selected text.
selectmodeDetermines how many items can be selected, and how mouse drags affect the selection:
  • BROWSE: Normally, you can only select one line out of a listbox. If you click on an item and then drag to a different line, the selection will follow the mouse. This is the default.

  • SINGLE: You can only select one line, and you can't drag the mouse—wherever you click button 1, that line is selected.

  • MULTIPLE: You can select any number of lines at once. Clicking on any line toggles whether or not it is selected.

  • EXTENDED: You can select any adjacent group of lines at once by clicking on the first line and dragging to the last line.

takefocusNormally, 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”.
widthThe 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.
xscrollcommandIf 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.
yscrollcommandIf 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:

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, last], 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 first is deleted.

.get ( first, last=None )

Returns a tuple containing the text of the lines with indices from first to last, inclusive. If the second argument is omitted, returns the text of the line closest to first.

.index ( i )

If possible, positions the visible part of the listbox so that the line containing index i is at the top of the widget.

.insert ( index, *elements )

Insert one or more new lines into the listbox before the line specified by index. Use 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 index is visible.

.selection_clear ( first, last=None )

Unselects all of the lines between indices first and last, inclusive. If the second argument is omitted, unselects the line with index first.

.selection_includes ( index )

Returns 1 if the line with the given index is selected, else returns 0.

.selection_set ( first, last=None )

Selects all of the lines between indices first and last, inclusive. If the second argument is omitted, selects the line with index 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 fraction of the width of its longest line is outside the left side of the listbox. Fraction is in the range [0,1].

.xview_scroll ( number, what )

Scrolls the listbox horizontally. For the what argument, use either UNITS to scroll by characters, or PAGES to scroll by pages, that is, by the width of the listbox. The number argument tells how many to scroll; negative values move the text to the right within the listbox, positive values leftward.

.yview()

To make the listbox vertically scrollable, set the command option of the associated vertical scrollbar to this method. See Section 11.1, “Scrolling a Listbox widget”.

.yview_moveto ( fraction )

Scroll the listbox so that the top fraction of the width of its longest line is outside the left side of the listbox. Fraction is in the range [0,1].

.yview_scroll ( number, what )

Scrolls the listbox vertically. For the what argument, use either UNITS to scroll by lines, or PAGES to scroll by pages, that is, by the height of the listbox. The number argument tells how many to scroll; negative values move the text downward inside the listbox, and positive values move the text up.