We're back after a server migration that caused effbot.org to fall over a bit harder than expected. Expect some glitches.

The Tkinter Listbox Class

Listbox(master=None, **options) (class) [#]

A scrolling listbox.

master
Parent widget.
**options
Widget options. See the description of the config method for a list of available options.

activate(index) [#]

Activates the given index (it will be marked with an underline). The active item can be refered to using the ACTIVE index.

index
Index specifier.

bbox(self, index) [#]

Gets the bounding box of the given item text.

index
Index specifier.
Returns:
The bounding box, as a 4-tuple (xoffset, yoffset, width, height). If the item is not visible, this method returns None. If the item is partially visible, the box may extend outside the visible area.

config(**options) [#]

Modifies one or more widget options. If no options are given, the method returns a dictionary containing all current option values.

**options
Widget options.
activestyle=
Default is underline. (the option database name is activeStyle, the class is ActiveStyle)
background=
Default value is ‘SystemButtonFace’. (background/Background)
bg=
Same as background.
borderwidth=
Default value is 2. (borderWidth/BorderWidth)
bd=
Same as borderwidth.
cursor=
No default value. (cursor/Cursor)
disabledforeground=
Default is system specific. (disabledForeground/DisabledForeground)
exportselection=
Default value is 1. (exportSelection/ExportSelection)
font=
Default value is system specific. (font/Font)
foreground=
Default value is system specific. (foreground/Foreground)
fg=
Same as foreground.
height=
Default value is 10. (height/Height)
highlightbackground=
Default value is system specific. (highlightBackground/HighlightBackground)
highlightcolor=
Default value is system specific. (highlightColor/HighlightColor)
highlightthickness=
Default value is 1. (highlightThickness/HighlightThickness)
listvariable=
No default value. (listVariable/Variable)
relief=
Default is SUNKEN. (relief/Relief)
selectbackground=
Default is system specific. (selectBackground/Foreground)
selectborderwidth=
Default is 1. (selectBorderWidth/BorderWidth)
selectforeground=
Default is system specific. (selectForeground/Background)
selectmode=
Default is BROWSE. (selectMode/SelectMode)
setgrid=
Default is 0. (setGrid/SetGrid)
state=
Default is NORMAL. (state/State)
takefocus=
No default value. (takeFocus/TakeFocus)
width=
Default is 20. (width/Width)
xscrollcommand=
No default value. (xScrollCommand/ScrollCommand)
yscrollcommand=
No default value. (yScrollCommand/ScrollCommand)

curselection() [#]

Gets a list of the currently selected alternatives. The list contains the indexes of the selected alternatives (beginning with 0 for the first alternative in the list).
In most Python versions, the list contains strings instead of integers. Since this may change in future versions, you should make sure your code can handle either case. See the patterns section for a suggested solution.

Returns:
A list of index specifiers.

delete(first, last=None) [#]

Deletes one or more items. Use delete(0, END) to delete all items in the list.

first
First item to delete.
last
Last item to delete. If omitted, a single item is deleted.

get(first, last=None) [#]

Gets one or more items from the list. This function returns the string corresponding to the given index (or the strings in the given index range). Use get(0, END) to get a list of all items in the list. Use get(ACTIVE) to get the active (underlined) item.

first
First item to return.
last
Last item to return. If omitted, a single item is returned.
Returns:
A list of strings.

index(index) [#]

Returns the numerical index (0 to size()-1) corresponding to the given index. This is typically ACTIVE, but can also be ANCHOR, or a string having the form “@x,y” where x and y are widget-relative pixel coordinates.

index
Index specifier.
Returns:
Numerical index.

insert(index, *elements) [#]

Inserts one or more items at given index (this works as for Python lists; index 0 is before the first item). Use END to append items to the list. Use ACTIVE to insert items before the the active (underlined) item.

index
Index specifier.
*elements
One or more elements to add.

itemcget(index, option) [#]

Gets a configuration option for an individual listbox item.

index
option

itemconfig(index, **options) [#]

Modifies the configuration for an individual listbox item.

index
**options

itemconfig(index, **options) [#]

Same as itemconfig.

nearest(y) [#]

Returns the index nearest to the given coordinate (a widget-relative pixel coordinate).

y
Coordinate.
Returns:
An index.

scan_dragto(x, y) [#]

Scrolls the widget contents according to the given mouse coordinate. The text is moved 10 times the distance between the scanning anchor and the new position.

x
Mouse coordinate.
y
Mouse coordinate.

scan_mark(x, y) [#]

Sets the scanning anchor for fast horizontal scrolling to the given mouse coordinate.

x
Mouse coordinate.
y
Mouse coordinate.

see(index) [#]

Makes sure the given list index is visible. You can use an integer index, or END.

index
Index specifier.

select_anchor(index) [#]

Same as selection_anchor.

select_clear(first, last=None) [#]

Same as selection_clear.

select_includes(index) [#]

Same as selection_includes.

select_set(first, last=None) [#]

Same as selection_set.

selection_anchor(index) [#]

Sets the selection anchor to the given index. The anchor can be refered to using the ANCHOR index.

index
Index specifier.

selection_clear(first, last=None) [#]

Removes one or more items from the selection.

first
First item to remove.
last
Last item to remove. If omitted, only one item is removed.

selection_includes(index) [#]

Checks if an item is selected.

index
Index specifier.
Returns:
A true value if the item is selected.

selection_set(first, last=None) [#]

Adds one or more items to the selection.

first
First item to add.
last
Last item to add. If omitted, only one item is added.

size() [#]

Returns the number of items in the list. The valid index range goes from 0 to size()-1.

Returns:
The number of items in this list.

xview(column, *extra) [#]

Controls horizontal scrolling.

If called without an arguement, this method determines which part of the full list that is visible in the horizontal direction. This is given as the offset and size of the visible part, given in relation to the full size of the list (1.0 is the full list).

If called with a single argument, this method adjusts the list so that the given character column is at the left edge of the listbox.

If called with the string “moveto” and a fraction, this method behaves like xview_moveto. If called with the string “scroll” and two more arguments, this method behaves like xview_scroll.

column
The column to place at the left edge, or a string specifying what subcommand to execute.
*extra
Additional arguments for the “moveto” and “scroll” forms. See above for details.
Returns:
If called without any arguments, a 2-tuple containing the left offset and the view size (relative to the full width).

xview_moveto(fraction) [#]

Adjusts the list so that the given offset is at the left (top) edge of the listbox. Offset 0.0 is the beginning of the list, 1.0 the end. These methods are used by the Scrollbar bindings when the user drags the scrollbar slider.

fraction
Offset.

xview_scroll(number, what) [#]

Scrolls the list view horizontally by the given amount.

number
Number of units.
what
What unit to use. This can be either “units” (characters) or “pages” (larger steps).

yview(*what) [#]

Controls vertical scrolling. This method works like xview, but controls vertical scrolling.

To make sure that a given item is visible, use the see method.

index
The index to place at the top edge, or a string specifying what subcommand to execute.
*extra
Additional arguments for the “moveto” and “scroll” forms.
Returns:
If called without any arguments, a 2-tuple containing the top offset and the view size (relative to the list size).

yview_moveto(fraction) [#]

Adjusts the list view so that the given offset is at the left edge of the canvas. Offset 0.0 is the beginning of the entry string, 1.0 the end.

fraction
Offset.

yview_scroll(number, what) [#]

Scrolls the list view vertically by the given amount.

number
Number of units.
what
What unit to use. This can be either “units” (characters) or “pages” (larger steps).