beetlebug▾
/

ttk::treeview

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

CommandDescription
ttk::treeview insertAdds a new item and gives its ID.
ttk::treeview itemGives or sets the options of an item.
ttk::treeview setGives or sets the values of an item in the data columns.
ttk::treeview childrenGives or replaces the children of an item.
ttk::treeview parentGives the parent of an item.
ttk::treeview nextGives the next sibling of an item.
ttk::treeview prevGives the previous sibling of an item.
ttk::treeview indexGives the place of an item in the children of its parent.
ttk::treeview existsTests whether an item exists.
ttk::treeview moveMoves an item to a new parent or a new place.
ttk::treeview detachTakes items out of the tree and keeps them.
ttk::treeview deleteDeletes items and their children.
ttk::treeview focusGives or sets the focus item.
ttk::treeview selectionGives or changes the selected items.
ttk::treeview seeScrolls the tree view and opens the parents of an item so that the item shows.
ttk::treeview bboxGives the box of the row of an item, or of one cell.
ttk::treeview identifyGives the part of the tree view at a point.
ttk::treeview columnGives or sets the options of a column.
ttk::treeview headingGives or sets the options of the heading of a column.
ttk::treeview tagAdds, removes, and lists the tags of items, and gives a tag a look and bindings.
ttk::treeview dragMoves the right edge of a column to a pixel, as a drag of the separator of its heading does.
ttk::treeview dropEnds a drag of a column separator.
ttk::treeview xviewGives or moves the horizontal view of the columns.
ttk::treeview yviewGives or moves the vertical view of the rows.
ttk::treeview stateGives the state flags of the widget, or turns flags on and off.
ttk::treeview instateTests the state flags of the widget, and runs a script when they match.
ttk::treeview cgetGives the value of an option of the widget.
ttk::treeview configureGives or sets the options of the widget.

Options

OptionTypeDefaultDatabase nameDescription
-classclass nameemptyThe class of the widget for the option database and for the bindings, set only at creation.
-columnslist of column IDsemptycolumnsThe IDs of the data columns.
-cursorcursor nameemptycursorThe shape of the pointer over the widget.
-displaycolumnslist of columns#alldisplayColumnsThe data columns to show, and their order.
-emptytextstringemptyemptyTextA text that the tree view shows while it has no items.
-heightinteger10heightThe count of rows that the tree view asks room for.
-paddinglist of distancesemptypaddingThe space between the border and the rows.
-selectmodemodeextendedselectModeHow the user can select items with the pointer and the keys.
-showlist of partstree headingsshowThe parts that the tree view shows: tree, headings, or both.
-stylestyle nameemptystyleThe style of the widget, such as Treeview or a style of the script such as Accent.Treeview.
-takefocusboolean, empty, or scriptttk::takefocustakeFocusWhether the Tab key can move the focus to the widget.
-xscrollcommandscriptemptyxScrollCommandThe command prefix that the tree view calls when its horizontal view changes, for a scrollbar.
-yscrollcommandscriptemptyyScrollCommandThe 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.

ttk::treeview with -height 4 and -columns {}-columns {}ttk::treeview with -height 4 and -columns size-columns size
Each with -height 4.
% ttk::treeview .ttvco -columns {size date}
> .ttvco insert {} end -id f -values {10 today}
> .ttvco set f date
today

Each list is a valid list of IDs.

-cursor CURSOR

The shape of the pointer over the widget.

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
hand2

ztk 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 files

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

ttk::treeview with -columns size and -height 2-height 2ttk::treeview with -columns size and -height 4-height 4
Each with -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.

ttk::treeview with -height 4 -columns size and -padding {}-padding {}ttk::treeview with -height 4 -columns size and -padding 4-padding 4ttk::treeview with -height 4 -columns size and -padding 16-padding 16
Each with -height 4 -columns size.
% ttk::treeview .ttvpa
> ttk::treeview .ttvpa2 -padding 10
> expr {[winfo reqwidth .ttvpa2] > [winfo reqwidth .ttvpa]}
1

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

ValueDescription
extendedThe user can select more than one item, with Shift and Control.
browseThe user can select one item at a time.
noneThe 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.

ttk::treeview with -height 4 -columns size and -show {tree headings}-show {tree headings}ttk::treeview with -height 4 -columns size and -show tree-show treettk::treeview with -height 4 -columns size and -show headings-show headings
Each with -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.Treeview

ztk 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
0

ztk 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.0

Each 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}
1

Each 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

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztk 1.0.0-beta.1

See also