ttk::treeview
Guide: Tree
Makes a themed tree view: a list of items in a tree, with columns of values and a row of headings.
ttk::treeview PATHNAME ?OPTION VALUE ...?
Description
Makes a themed tree view with the name PATHNAME and the options of the call, and makes the command PATHNAME for the widget commands below. Each item has an ID, a -text in the tree column #0, a list of -values for the data columns, and child items. An item with children opens and closes with a click on its indicator.
The root item has the ID {}. insert adds an item and gives its ID. A column is named by its ID from -columns, by #N for the display column N, or by #0 for the tree column. Tags give items a look with tag configure and bindings with tag bind.
The tree view sends the virtual event <<TreeviewSelect>> when the selection changes, and <<TreeviewOpen>> and <<TreeviewClose>> when the user opens or closes an item. The parent of PATHNAME must exist, and PATHNAME must not. The class of the tree view is Treeview.
Widget commands
| Command | Description |
|---|---|
ttk::treeview insert | Adds a new item and gives its ID. |
ttk::treeview item | Gives or sets the options of an item. |
ttk::treeview set | Gives or sets the values of an item in the data columns. |
ttk::treeview children | Gives or replaces the children of an item. |
ttk::treeview parent | Gives the parent of an item. |
ttk::treeview next | Gives the next sibling of an item. |
ttk::treeview prev | Gives the previous sibling of an item. |
ttk::treeview index | Gives the place of an item in the children of its parent. |
ttk::treeview exists | Tests whether an item exists. |
ttk::treeview move | Moves an item to a new parent or a new place. |
ttk::treeview detach | Takes items out of the tree and keeps them. |
ttk::treeview delete | Deletes items and their children. |
ttk::treeview focus | Gives or sets the focus item. |
ttk::treeview selection | Gives or changes the selected items. |
ttk::treeview see | Scrolls the tree view and opens the parents of an item so that the item shows. |
ttk::treeview bbox | Gives the box of the row of an item, or of one cell. |
ttk::treeview identify | Gives the part of the tree view at a point. |
ttk::treeview column | Gives or sets the options of a column. |
ttk::treeview heading | Gives or sets the options of the heading of a column. |
ttk::treeview tag | Adds, removes, and lists the tags of items, and gives a tag a look and bindings. |
ttk::treeview drag | Moves the right edge of a column to a pixel, as a drag of the separator of its heading does. |
ttk::treeview drop | Ends a drag of a column separator. |
ttk::treeview xview | Gives or moves the horizontal view of the columns. |
ttk::treeview yview | Gives or moves the vertical view of the rows. |
ttk::treeview state | Gives the state flags of the widget, or turns flags on and off. |
ttk::treeview instate | Tests the state flags of the widget, and runs a script when they match. |
ttk::treeview cget | Gives the value of an option of the widget. |
ttk::treeview configure | Gives or sets the options of the widget. |
Options
| Option | Type | Default | Database name | Description |
|---|---|---|---|---|
-class | class name | empty | The class of the widget for the option database and for the bindings, set only at creation. | |
-columns | list of column IDs | empty | columns | The IDs of the data columns. |
-cursor | cursor name | empty | cursor | The shape of the pointer over the widget. |
-displaycolumns | list of columns | #all | displayColumns | The data columns to show, and their order. |
-emptytext | string | empty | emptyText | A text that the tree view shows while it has no items. |
-height | integer | 10 | height | The count of rows that the tree view asks room for. |
-padding | list of distances | empty | padding | The space between the border and the rows. |
-selectmode | mode | extended | selectMode | How the user can select items with the pointer and the keys. |
-show | list of parts | tree headings | show | The parts that the tree view shows: tree, headings, or both. |
-style | style name | empty | style | The style of the widget, such as Treeview or a style of the script such as Accent.Treeview. |
-takefocus | boolean, empty, or script | ttk::takefocus | takeFocus | Whether the Tab key can move the focus to the widget. |
-xscrollcommand | script | empty | xScrollCommand | The command prefix that the tree view calls when its horizontal view changes, for a scrollbar. |
-yscrollcommand | script | empty | yScrollCommand | The command prefix that the tree view calls when its vertical view changes, for a scrollbar. |
-class CLASS
The class of the widget for the option database and for the bindings, set only at creation.
The class name replaces the class of the style, such as Treeview, in winfo class and in the bind tags of the widget. Give it in the call that makes the widget. A later configure of the option gives an error.
% ttk::treeview .ttvcl -class Fancy
> winfo class .ttvcl
Fancy
% ttk::treeview .ttvcl3
> winfo class .ttvcl3
Treeview% ttk::treeview .ttvcl2
> .ttvcl2 configure -class Other
attempt to change read-only option
-errorcode NONE-columns LIST
The IDs of the data columns.
Each element is the ID of one data column, in the order of the -values of an item. The tree column #0 is not in the list. A new list of columns resets -displaycolumns to #all.

-columns {}
-columns size-height 4.% ttk::treeview .ttvco -columns {size date}
> .ttvco insert {} end -id f -values {10 today}
> .ttvco set f date
todayEach list is a valid list of IDs.
-cursor CURSOR
The shape of the pointer over the widget.
Each value side by side: the contact sheet of the cursors
A cursor name of Tk, such as hand2 or xterm. An empty value uses the cursor of the parent.
% ttk::treeview .ttvcu -cursor hand2
> .ttvcu cget -cursor
hand2ztk stores each value of the option with no check.
-displaycolumns LIST
The data columns to show, and their order.
A list of column IDs or indexes in the order to show them, or #all for each column of -columns in its order. A column that is not in the list keeps its values and does not show.
% ttk::treeview .ttvdc -columns {a b c} -displaycolumns {c a}
> .ttvdc cget -displaycolumns
c a% ttk::treeview .ttvdc2 -columns {a b} -displaycolumns {zz}
Invalid column index zz
-errorcode NONE-emptytext TEXT
A text that the tree view shows while it has no items.
The text shows in the area of the rows while the root item has no children.
% ttk::treeview .ttvet -emptytext "No files"
> .ttvet cget -emptytext
No filesEach string is a valid text.
-height ROWS
The count of rows that the tree view asks room for.
The requested height is this count of rows, with the row of headings. A geometry manager can give the tree view another height.

-height 2
-height 4-columns size.% ttk::treeview .ttvhe -height 5
> ttk::treeview .ttvhe2 -height 10
> expr {[winfo reqheight .ttvhe2] > [winfo reqheight .ttvhe]}
1% ttk::treeview .ttvhe3 -height x
bad screen distance "x"
-errorcode TK VALUE PIXELS-padding PADDING
The space between the border and the rows.
A list of 1 to 4 distances: left, top, right, and bottom. A missing third value is the first, and a missing fourth value is the second. An empty value uses the padding of the style.

-padding {}
-padding 4
-padding 16-height 4 -columns size.% ttk::treeview .ttvpa
> ttk::treeview .ttvpa2 -padding 10
> expr {[winfo reqwidth .ttvpa2] > [winfo reqwidth .ttvpa]}
1ztk stores each value at configure, as Tk 8.6.16 does.
-selectmode MODE
How the user can select items with the pointer and the keys.
The mode limits the selection of the user. The selection command of a script can select each item in each mode.
| Value | Description |
|---|---|
extended | The user can select more than one item, with Shift and Control. |
browse | The user can select one item at a time. |
none | The user cannot change the selection. |
% ttk::treeview .ttvsm -selectmode browse
> .ttvsm cget -selectmode
browse% ttk::treeview .ttvsm2 -selectmode bogus
bad selectmode "bogus": must be none, browse, or extended
-errorcode TCL LOOKUP INDEX selectmode bogus-show LIST
The parts that the tree view shows: tree, headings, or both.
tree shows the tree column #0, with the text and the indicator of each item. headings shows the row of headings. An empty list shows only the data columns and no headings, such as for a table.

-show {tree headings}
-show tree
-show headings-height 4 -columns size.% ttk::treeview .ttvsh -show headings
> .ttvsh cget -show
headings% ttk::treeview .ttvsh2 -show bogus
bad value "bogus": must be tree or headings
-errorcode NONE-style STYLE
The style of the widget, such as Treeview or a style of the script such as Accent.Treeview.
An empty value uses the style Treeview. A style name of the script ends with the name of the base style, such as Accent.Treeview, and ttk::style configure and ttk::style map give it its options. A style with no settings looks like its base style.
% ttk::style configure Accent.Treeview -foreground white
> ttk::treeview .ttvsy -style Accent.Treeview
> .ttvsy cget -style
Accent.Treeviewztk stores each value of the option with no check, as Tk 8.6.16 does at creation.
-takefocus VALUE
Whether the Tab key can move the focus to the widget.
1 puts the widget in the order of the Tab key, and 0 leaves it out. An empty value uses the default rule of Tk. A script, such as the default ttk::takefocus, runs with the path of the widget appended, and its result is the answer. ttk::takefocus gives 1 when the widget is not disabled and is viewable.
% ttk::treeview .ttvtf -takefocus 0
> .ttvtf cget -takefocus
0ztk stores each value of the option with no check.
-xscrollcommand PREFIX
The command prefix that the tree view calls when its horizontal view changes, for a scrollbar.
The tree view calls the prefix with two fractions appended: the start and the end of the visible part. Give it the set command of a horizontal scrollbar.
% ttk::treeview .ttvxs -xscrollcommand {lappend ::ttvx}
> pack .ttvxs
> update
> lrange $::ttvx end-1 end
0.0 1.0Each string is a valid prefix.
-yscrollcommand PREFIX
The command prefix that the tree view calls when its vertical view changes, for a scrollbar.
The tree view calls the prefix with two fractions appended: the start and the end of the visible part of the rows. Give it the set command of a vertical scrollbar.
% ttk::treeview .ttvys -height 5 -yscrollcommand {lappend ::ttvy}
> for {set i 0} {$i < 20} {incr i} {.ttvys insert {} end}
> pack .ttvys
> update
> lassign [lrange $::ttvy end-1 end] first last
> expr {$last < 1}
1Each string is a valid prefix.
Returns
PATHNAME.
Examples
% ttk::treeview .files -columns {size}
> .files heading #0 -text Name
> .files heading size -text Size
> .files insert {} end -id docs -text docs -open 1
> .files insert docs end -id readme -text README.md -values {2048}
> list [.files children docs] [.files set readme size]
readme 2048
% winfo class [ttk::treeview .tree]
Treeview
Errors
% ttk::treeview .x -text a
unknown option "-text"
-errorcode TK LOOKUP OPTION -text
% ttk::treeview .twice
> ttk::treeview .twice
window name "twice" already exists in parent
-errorcode TK VALUE WINDOW EXISTS
# The parent .no does not exist.
% ttk::treeview .no.t
bad window path name ".no"
-errorcode TK LOOKUP WINDOW .no
% ttk::treeview
wrong # args: should be "ttk::treeview pathName ?-option value ...?"
-errorcode TCL WRONGARGS
Differences from Tcl
-cursoraccepts each value with no check. Tk 8.6.16 gives an error for a cursor name that does not exist.- The errors of
state,instate,identify, and of an unknown widget command have the error codeNONE. Tk 8.6.16 gives a code such asTTK VALUE STATEorTCL LOOKUP INDEX. The error of-underlinehas the codeTCL VALUE NUMBER, where Tk 8.6.16 givesTCL VALUE INTEGER. - The states
activeandhoverare one flag, sostate activeturns on both. - The errors of the widget commands, such as an item that does not exist or a bad column, have the error code
NONE. Tk 8.6.16 gives a code such asTTK TREE ITEMorTCL WRONGARGS. - Some messages of a wrong count of arguments differ from Tk 8.6.16, such as those of
column,heading,item,tag,exists, anddrag. A bad word ofselectiongives the message of a wrong count of arguments, where Tk 8.6.16 givesbad selection operation.dropaccepts arguments and makes no change. identify X Y, with no component, givesitem ID column COLUMNorheading COLUMN. Tk 8.6.16 gives the element in place of the column.-emptytextis an addition of ztk. Tk 8.6.16 does not have it.-padding 10adds 36 pixels to the requested width. Tk 8.6.16 adds 20 pixels.- Before the tree view is on the screen,
xviewgives0.0 0.0. Tk 8.6.16 gives0.0 1.0. - A row has the height of the theme, so
-heightgives a taller widget than in Tk 8.6.16.
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztk 1.0.0-beta.1