ttk.Treeview - hierarchical multicolumn data display widget

SYNOPSIS

instance = ttk.Treeview([**options])

DESCRIPTION

The ttk.Treeview widget displays a hierarchical collection of items. Each item has a textual label, an optional image, and an optional list of data values. The data values are displayed in successive columns after the tree label.

The order in which data values are displayed may be controlled by setting the displaycolumns widget option. The Tree widget can also display column headings. Columns may be accessed by number or by symbolic names listed in the columns widget option; see COLUMN IDENTIFIERS.

Each item is identified by a unique name. The widget will generate item IDs if they are not supplied by the caller. There is a distinguished root item, named "". The root item itself is not displayed; its children appear at the top level of the hierarchy.

Each item also has a list of tags, which can be used to associate event bindings with individual items and control the appearance of the item.

Treeview widgets support horizontal and vertical scrolling with the standard [xy]scrollcommand options and [xy]view() widget methods.

STANDARD OPTIONS

class
cursor
padding
style
takefocus
xscrollcommand
yscrollcommand

WIDGET-SPECIFIC OPTIONS

columns
A list of column identifiers, specifying the number of columns and their names.

displaycolumns
A list of column identifiers (either symbolic names or integer indices) specifying which data columns are displayed and the order in which they appear, or the string "#all". If set to "#all" (the default), all columns are shown in the order given.

height
Specifies the number of rows which should be visible. Note: the requested width is determined from the sum of the column widths.

selectmode
Controls how the built-in class bindings manage the selection. One of "extended", "browse", or "none".
If set to "extended" (the default), multiple items may be selected. If "browse", only a single item will be selected at a time. If "none", the selection will not be changed.
Note that application code and tag bindings can set the selection however they wish, regardless of the value of selectmode.

show
A list containing zero or more of the following values, specifying which elements of the tree to display.
  • "tree"
    Display tree labels in column #0.

  • "headings"
    Display the heading row.
The default is tree="headings", i.e., show all elements.
NOTE: Column #0 always refers to the tree column, even if show="tree" is not specified.

WIDGET METHOD

instance.cget("option")
Returns the current value of the specified option; see ttk.Widget().

instance.column(column)
instance.column(column ,"option")
instance.column(column ,**options)
Query or modify the options for the specified column.
If no option is specified, returns a dictionary of option/value pairs.
If a single "option" is specified, returns the value of that option.
Otherwise, the options are updated with the specified values.
The following options may be set on each column:
  • anchor
    Specifies how the text in this column should be aligned with respect to the cell. One of "n", "ne", "e", "se", "s", "sw", "w", "nw", or "center".

  • minwidth
    The minimum width of the column in pixels. The Treeview widget will not make the column any smaller than minwidth when the widget is resized or the user drags a column separator.

  • stretch
    Specifies whether or not the column's width should be adjusted when the widget is resized.

  • width=w
    The width of the column in pixels. Default is something reasonable, probably 200 or so.
Use pathname column #0 to configure the tree column.

instance.delete(itemList)
Deletes each of the items in itemList and all of their descendants. The root item may not be deleted. See also: detach().

instance.detach(itemList)
Unlinks all of the specified items in itemList from the tree. The items and all of their descendants are still present and may be reinserted at another point in the tree with the move() operation, but will not be displayed until that is done. The root item may not be detached. See also: delete().

instance.exists(item)
Returns True if the specified item is present in the tree, False otherwise.

instance.heading(column ,**options)
Query or modify the heading options for the specified column. Valid options are:
  • text="text"
    The text to display in the column heading.

  • image=imageName
    Specifies an image to display to the right of the column heading.

  • anchor="anchor"
    Specifies how the heading text should be aligned. One of the standard Tkinter anchor values.

  • command=callback
    A callback to evaluate when the heading label is pressed.
Use instance.heading("#0") to configure the tree column heading.

instance.identify(component ,x ,y)
Returns a description of the specified component under the point given by x and y, or the empty string if no such component is present at that position.
The following submethods are supported:
  • instance.identify_region(x ,y)
    Returns one of:

    • heading
      Tree heading area.

    • separator
      Space between two column headings.

    • tree
      The tree area.

    • cell
      A data cell.

  • instance.identify_column(x ,y)
    Returns the data column identifier of the cell at position x. The tree column has ID #0.

  • instance.identify_element(x ,y)
    The element at position x,y.

  • instance.identify_row(x ,y)
    Returns the item ID of the item at position y.
See COLUMN IDENTIFIERS for a discussion of display columns and data columns.

instance.index(item)
Returns the integer index of item within its parent's list of children.

instance.insert(parent ,index [,iid="id"] ,**options)
Creates a new item. parent is the item ID of the parent item, or the empty string "" to create a new top-level item. index is an integer, or the value "end", specifying where in the list of parent's children to insert the new item. If index is less than or equal to zero, the new node is inserted at the beginning; if index is greater than or equal to the current number of children, it is inserted at the end. If iid is specified, it is used as the item identifier; "id" must not already exist in the tree. Otherwise, a new unique identifier is generated.
See ITEM OPTIONS for the list of available options.

instance.item(item)
instance.item(item ,"option")
instance.item(item ,**options)
Query or modify the options for the specified item. If no option is specified, returns a dictionary of option/value pairs. If a single "option" is specified, returns the value of that option. Otherwise, the item's options are updated with the specified values. See ITEM OPTIONS for the list of available options.

instance.move(item ,parent ,index)
Moves item to position index in parent's list of children. It is illegal to move an item under one of its descendants.
If index is less than or equal to zero, item is moved to the beginning; if greater than or equal to the number of children, it is moved to the end.

instance.next(item)
Returns the identifier of item's next sibling, or "" if item is the last child of its parent.

instance.parent(item)
Returns the ID of the parent of item, or "" if item is at the top level of the hierarchy.

instance.prev(item)
Returns the identifier of item's previous sibling, or "" if item is the first child of its parent.

instance.see(item)
Ensure that item is visible: sets all of item's ancestors to open=True, and scrolls the widget if necessary so that item is within the visible portion of the tree.

instance.selection([selop ,items=itemList])
If selop is not specified, returns the list of selected items. Otherwise, selop is one of the following:
  • instance.selection_set(itemList)
    itemList becomes the new selection.

  • instance.selection_add(itemList)
    Add itemList to the selection

  • instance.selection_remove(itemList)
    Remove itemList from the selection

  • instance.selection_toggle(itemList)
    Toggle the selection state of each item in itemList.

instance.set(item [,column [,value]])
With one argument, returns a dictionary of column/value pairs for the specified item. With two arguments, returns the current value of the specified column. With three arguments, sets the value of column column in item item to the specified value. See also COLUMN IDENTIFIERS.

instance.tag_configure(tagName)
instance.tag_configure(tagName ,"option")
instance.tag_configure(tagName ,**options)
Query or modify the options for the specified tagName.
If one or more option/value pairs are specified, sets the value of those options for the specified tag.
If a single option is specified, returns the value of that option (or the empty string if the option has not been specified for tagName).
With no additional arguments, returns a dictionary of the option settings for tagName. See TAG OPTIONS for the list of available options.

instance.tag_has(tagName [,item])
If item is specified, returns True or False depending on whether the specified item has the named tag. Otherwise, returns a list of all items which have the specified tag.

ITEM OPTIONS

The following item options may be specified for items in the insert() and item() widget methods.

text
The textual label to display for the item.

image
A Tkinter image, displayed to the left of the label.

values
The list of values associated with the item.
Each item should have the same number of values as the columns widget option. If there are fewer values than columns, the remaining values are assumed empty. If there are more values than columns, the extra values are ignored.

open
A boolean value indicating whether the item's children should be displayed (open=True) or hidden (open=False).

tags
A list of tags associated with this item.

TAG OPTIONS

The following options may be specified on tags:

foreground
Specifies the text foreground color.

background
Specifies the cell or item background color.

font
Specifies the font to use when drawing text.

image Specifies the item image, in case the item's image option is empty.

COLUMN IDENTIFIERS

Column identifiers take any of the following forms:
  • A symbolic name from the list of columns.

  • An integer n, specifying the nth data column.

  • A string of the form "#n", where n is an integer, specifying the nth display column.
NOTE: Item values may be displayed in a different order than the order in which they are stored.
NOTE: Column #0 always refers to the tree column, even if show="tree" is not specified.
A data column number is an index into an item's values list; a display column number is the column number in the tree where the values are displayed. Tree labels are displayed in column #0. If displaycolumns is not set, then data column n is displayed in display column #n+1. Again, column #0 always refers to the tree column.

VIRTUAL EVENTS

The Treeview widget generates the following virtual events.

<<TreeviewSelect>>
Generated whenever the selection changes.

<<TreeviewOpen>>
Generated just before setting the focus item to open=True.

<<TreeviewClose>>
Generated just after setting the focus item to open=False.

The focus() and selection() widget methods can be used to determine the affected item or items.

Comments