Tkinter入门
前页 | Chapter 31. The Listbox Widget | 后页

# Methods

The `Listbox` widget supports the standard Tkinter Widget interface, plus the following methods:

`activate(index)`

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

`bbox(index) => tuple or None`

  Get the bounding box of the given item text. The bounding box is returned as a 4-tuple giving (xoffset, yoffset, width, height). If the item is not visible, this method returns `None`.

`curselection() => list`

  Get 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 Python 1.5.2 and earlier, 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.

`delete(index)`, `delete(first, last)`

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

`get(index) => string`, `get(first, last) => list`

  Get 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 `ACTIVE` to get the active (underlined) item.

`index(index) => integer`

  Return 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.

`insert(index, items)`

  Insert 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.

`nearest(y) => string`

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

`see(index)`

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

`size() => integer`

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

## Selection Methods

The following methods are used to manipulate the listbox selection.

`select_adjust(index)`

  Extend the selection to include the given index.

`select_anchor(index)`

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

`select_clear()`

  Clear the selection.

`select_includes(index) => flag`

  Returns true (non-zero) if the given item is selected.

`select_set(index)`, `select_set(first, last)`

  Add one or more items to the selection.

## Scrolling Methods

These methods are used to scroll the listbox widget in various ways. The `scan` methods can be used to implement fast mouse scrolling operations (they are bound to the middle mouse button, if available), while the `yview` method is used with a standard scrollbar widget.

`scan_mark(x, y)`

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

`scan_dragto(x, y)`

  Scroll 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.

`xview() => tuple`, `yview() => tuple`

  Determine which part of the full list that is visible in the horizontal (vertical) 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). These methods are used by the `Scrollbar` bindings.

`xview(column)`, `yview(index)`

  Adjust the list so that the given character column (list item) is at the left (top) edge of the listbox. To make sure that a given item is visible, use the `see` method instead.

`xview(MOVETO, offset)`, `yview(MOVETO, offset)`

  Adjust 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.

  The `MOVETO` constant is not defined in Python 1.5.2 and earlier. For compatibility, use the string "moveto" instead.

`xview(SCROLL, step, what)`, `yview(SCROLL, step, what)`

  Scroll the list horizontally (vertically) by the given amount. The what argument can be either `UNITS` (lines) or `PAGES`. These methods are used by the `Scrollbar` bindings when the user clicks on a scrollbar arrow or in the trough.

  These constants are not defined in Python 1.5.2 and earlier. For compatibility, use the strings "scroll", "units", and "pages" instead.

前页 | Home | 后页
Patterns | Up | Options