Treeview¶
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 widget option displaycolumns. The tree widget can also display column
headings. Columns may be accessed by number or symbolic names listed in the
widget option columns. 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.
The Treeview widget supports horizontal and vertical scrolling, according to
the options described in Scrollable Widget Options and the methods
Treeview.xview() and Treeview.yview().
选项¶
This widget accepts the following specific options:
选项
描述
columns
A list of column identifiers, specifying the number of
columns and their names.
displaycolumns
A list of column identifiers (either symbolic or
integer indices) specifying which data columns are
displayed and the order in which they appear, or the
string "#all".
height
Specifies the number of rows which should be visible.
Note: the requested width is determined from the sum
of the column widths.
padding
Specifies the internal padding for the widget. The
padding is a list of up to four length specifications.
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 the application code and tag bindings can set
the selection however they wish, regardless of the
value of this option.
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.
Item Options¶
The following item options may be specified for items in the insert and item
widget commands.
选项
描述
文本
The textual label to display for the item.
图片
A Tk Image, displayed to the left of the label.
值
The list of values associated with the item.
Each item should have the same number of values as the widget
option columns. 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
True/False value indicating whether the item's
children should be displayed or hidden.
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.
背景
Specifies the cell or item background color.
font
Specifies the font to use when drawing text.
图片
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 option.
An integer n, specifying the nth data column.
A string of the form #n, where n is an integer, specifying the nth display
column.
注释:
Item's option values may be displayed in a different order than the order
in which they are stored.
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 option 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 option displaycolumns is not set,
then data column n is displayed in column #n+1. Again, column #0 always
refers to the tree column.
Virtual Events¶
The Treeview widget generates the following virtual events.
Event
描述
<>
Generated whenever the selection changes.
<>
Generated just before settings the focus item to
open=True.
<>
Generated just after setting the focus item to
open=False.
The Treeview.focus() and Treeview.selection() methods can be used
to determine the affected item or items.
ttk.Treeview¶
classtkinter.ttk.Treeview¶
bbox(item, column=None)¶
Returns the bounding box (relative to the treeview widget's window) of
the specified item in the form (x, y, width, height).
If column is specified, returns the bounding box of that cell. If the
item is not visible (i.e., if it is a descendant of a closed item or is
scrolled offscreen), returns an empty string.
get_children(item=None)¶
Returns the list of children belonging to item.
If item is not specified, returns root children.
set_children(item, *newchildren)¶
Replaces item's child with newchildren.
Children present in item that are not present in newchildren are
detached from the tree. No items in newchildren may be an ancestor of
item. Note that not specifying newchildren results in detaching
item's children.
column(column, option=None, **kw)¶
Query or modify the options for the specified column.
If kw is not given, returns a dict of the column option values. If
option is specified then the value for that option is returned.
Otherwise, sets the options to the corresponding values.
The valid options/values are:
idReturns the column name. This is a read-only option.
anchor: One of the standard Tk anchor values.Specifies how the text in this column should be aligned with respect
to the cell.
minwidth: widthThe minimum width of the column in pixels. The treeview widget will
not make the column any smaller than specified by this option when
the widget is resized or the user drags a column.
stretch: True/False指明列宽度是否应该在部件大小被改变时进行相应的调整。
width: width以像素表示的列宽度。
要配置树的列,则调用此方法并附带参数 column = "#0"
delete(*items)¶
Delete all specified items and all their descendants.
The root item may not be deleted.
detach(*items)¶
Unlinks all of the specified items from the tree.
The items and all of their descendants are still present, and may be
reinserted at another point in the tree, but will not be displayed.
The root item may not be detached.
exists(item)¶
Returns True if the specified item is present in the tree.
focus(item=None)¶
If item is specified, sets the focus item to item. Otherwise, returns
the current focus item, or '' if there is none.
heading(column, option=None, **kw)¶
Query or modify the heading options for the specified column.
If kw is not given, returns a dict of the heading option values. If
option is specified then the value for that option is returned.
Otherwise, sets the options to the corresponding values.
The valid options/values are:
text: textThe text to display in the column heading.
image: imageNameSpecifies an image to display to the right of the column heading.
anchor: anchorSpecifies how the heading text should be aligned. One of the standard
Tk anchor values.
command: callbackA callback to be invoked when the heading label is pressed.
To configure the tree column heading, call this with column = "#0".
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.
identify_row(y)¶
Returns the item ID of the item at position y.
identify_column(x)¶
Returns the data column identifier of the cell at position x.
The tree column has ID #0.
identify_region(x, y)¶
Returns one of:
region
meaning
heading
Tree heading area.
separator
Space between two columns headings.
tree
The tree area.
cell
A data cell.
Availability: Tk 8.6.
identify_element(x, y)¶
Returns the element at position x, y.
Availability: Tk 8.6.
index(item)¶
Returns the integer index of item within its parent's list of children.
insert(parent, index, iid=None, **kw)¶
Creates a new item and returns the item identifier of the newly created
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; iid must not already exist in the tree.
Otherwise, a new unique identifier is generated.
See Item Options for the list of available points.
item(item, option=None, **kw)¶
Query or modify the options for the specified item.
If no options are given, a dict with options/values for the item is
returned.
If option is specified then the value for that option is returned.
Otherwise, sets the options to the corresponding values as given by kw.
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. If item
was detached it is reattached.
next(item)¶
Returns the identifier of item's next sibling, or '' if item is the
last child of its parent.
parent(item)¶
Returns the ID of the parent of item, or '' if item is at the top
level of the hierarchy.
prev(item)¶
Returns the identifier of item's previous sibling, or '' if item is
the first child of its parent.
reattach(item, parent, index)¶
see(item)¶
Ensure that item is visible.
Sets all of item's ancestors open option to True, and scrolls the
widget if necessary so that item is within the visible portion of
the tree.
selection()¶
Returns a tuple of selected items.
在 3.8 版更改:selection() no longer takes arguments. For changing the selection
state use the following selection methods.
selection_set(*items)¶
items becomes the new selection.
在 3.6 版更改:items can be passed as separate arguments, not just as a single tuple.
selection_add(*items)¶
Add items to the selection.
在 3.6 版更改:items can be passed as separate arguments, not just as a single tuple.
selection_remove(*items)¶
Remove items from the selection.
在 3.6 版更改:items can be passed as separate arguments, not just as a single tuple.
selection_toggle(*items)¶
Toggle the selection state of each item in items.
在 3.6 版更改:items can be passed as separate arguments, not just as a single tuple.
set(item, column=None, value=None)¶
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 given
column in given item to the specified value.
tag_bind(tagname, sequence=None, callback=None)¶
Bind a callback for the given event sequence to the tag tagname.
When an event is delivered to an item, the callbacks for each of the
item's tags option are called.
tag_configure(tagname, option=None, **kw)¶
Query or modify the options for the specified tagname.
If kw is not given, returns a dict of the option settings for
tagname. If option is specified, returns the value for that option
for the specified tagname. Otherwise, sets the options to the
corresponding values for the given tagname.
tag_has(tagname, item=None)¶
If item is specified, returns 1 or 0 depending on whether the specified
item has the given tagname. Otherwise, returns a list of all items
that have the specified tag.
Availability: Tk 8.6
xview(*args)¶
Query or modify horizontal position of the treeview.
yview(*args)¶
Query or modify vertical position of the treeview.