ttk::treeview(n)

🌳 ttk::treeview(n) β€” hierarchical multicolumn data display widget 🌳

okay so this is ttk::treeview(n), from the Tk Themed Widget manual, and it’s part 1 of 3, bestie β€” buckle up. πŸ’–


✨ NAME ✨

ttk::treeview β€” hierarchical multicolumn data display widget. periodt. πŸ’…


πŸŽ€ SYNOPSIS πŸŽ€

ttk::treeview pathname ?options?

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so 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. iykyk. 🌸

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 commands. no cap. πŸ¦‹


🌈 STANDARD OPTIONS 🌈

  • -class β€” standard option, bestie. See the ttk_widget manual entry for details.
  • -cursor β€” standard option. See the ttk_widget manual entry for details.
  • -takefocus β€” standard option. See the ttk_widget manual entry for details.
  • -style β€” standard option. See the ttk_widget manual entry for details.
  • -xscrollcommand β€” standard option. See the ttk_widget manual entry for details.
  • -yscrollcommand β€” standard option. See the ttk_widget manual entry for details.
  • -padding β€” standard option. See the ttk_widget manual entry for details.

See the ttk_widget manual entry for details on the standard options. πŸ’œ


πŸ’… WIDGET-SPECIFIC OPTIONS πŸ’…

  • -columns β€” Command-Line Name: -columns; Database Name: columns; Database Class: Columns. A list of column identifiers, specifying the number of columns and their names. πŸŽ€
  • -displaycolumns β€” Command-Line Name: -displaycolumns; Database Name: displayColumns; Database Class: 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 β€” Command-Line Name: -height; Database Name: height; Database Class: Height. Specifies the number of rows which should be visible. Note that the requested width is determined from the sum of the column widths. πŸ’–
  • -selectmode β€” Command-Line Name: -selectmode; Database Name: selectMode; Database Class: 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. the way that’s literally a rule. 🫢

  • -selecttype β€” Command-Line Name: -selecttype; Database Name: selectType; Database Class: SelectType. Controls how the built-in class bindings manage the selection. One of item or cell. 🌸
  • -show β€” Command-Line Name: -show; Database Name: show; Database Class: 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.

NOTE: Column #0 always refers to the tree column, even if -show tree is not specified. πŸ‘‘

  • -striped β€” Command-Line Name: -striped; Database Name: striped; Database Class: Striped. Boolean specifying zebra striped item coloring. Note that striped items uses the -stripedbackground option if set by the theme or a tag. If not supported by the current theme, it will not show. πŸ¦‹
  • -titlecolumns β€” Command-Line Name: -titlecolumns; Database Name: titleColumns; Database Class: TitleColumns. Number of display columns at the left that should not be scrolled. The tree column counts, even if -show tree is not specified. Thus for value N of this option, column #N is the first one that is scrollable. Default is 0. πŸŽ€
  • -titleitems β€” Command-Line Name: -titleitems; Database Name: titleItems; Database Class: TitleItems. Number of items at the top that should not be vertically scrolled. Default is 0. ✨

πŸ”₯ WIDGET COMMAND πŸ”₯

In addition to the standard cget, configure, instate, state, style, xview and yview commands (see ttk::widget), treeview widgets support the following additional commands:

  • pathname bbox item ?column? β€” Returns the bounding box (relative to the treeview widget's window) of the specified item in the form x y width height. If the item is not visible (i.e., if it is a descendant of a closed item or is vertically scrolled offscreen), returns the empty list. If column is specified and is not hidden (by the -displaycolumns option), returns the bounding box of that cell within item (even if the cell is horizontally scrolled offscreen). πŸ’–
  • pathname cellselection ?selop arg ...? β€” Manages cell selection. Cell selection is independent from item selection handled by the selection command. A cell is given by a list of two elements, item and column. For the rectangle versions of commands, the cells must be in displayed columns. Any change to -columns clears the cell selection. A cellList argument may be a single cell or a list of cells. If selop is not specified, returns the list of selected cells. Otherwise, selop is one of the following:
    • pathname cellselection set cellList β€” cellList becomes the new cell selection.
    • pathname cellselection set firstCell lastCell β€” The rectangle defined becomes the new cell selection.
    • pathname cellselection add cellList β€” Add cellList to the cell selection.
    • pathname cellselection add firstCell lastCell β€” The rectangle defined is added to the cell selection.
    • pathname cellselection remove cellList β€” Remove cellList from the cell selection.
    • pathname cellselection remove firstCell lastCell β€” The rectangle defined is removed from the cell selection.
    • pathname cellselection toggle cellList β€” Toggle the cell selection state of each cell in cellList. If the element is partially visible, the result gives the full area of the element, including any parts that are not visible.
    • pathname cellselection toggle firstCell lastCell β€” Toggle the cell selection state of each cell in the rectangle defined. 🌈
  • pathname children item ?newchildren? β€” If newchildren is not specified, returns the list of children belonging to item.

If newchildren is specified, replaces item's child list with newchildren. Items in the old child list not present in the new child list are detached from the tree. None of the items in newchildren may be an ancestor of item. πŸ¦‹

  • pathname column column ?-option ?value -option value...? β€” 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:
    • -id name β€” The column name. This is a read-only option. For example, [$pathname column #n -id] returns the data column associated with display column n. The tree column has -id #0.
    • -anchor anchor β€” Specifies how the text in this column should be aligned with respect to the cell. Anchor is one of n, ne, e, se, s, sw, w, nw, or center.
    • -minwidth 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 heading column separator. Default is 20 pixels.
    • -separator boolean β€” Specifies whether or not a column separator should be drawn to the right of the column. Default is false.
    • -stretch boolean β€” Specifies whether or not the column width should be adjusted when the widget is resized or the user drags a heading column separator. Boolean may have any of the forms accepted by Tcl_GetBoolean. By default columns are stretchable.
    • -width width β€” The width of the column in pixels. Default is 200 pixels. The specified column width may be changed by Tk in order to honor -stretch and/or -minwidth, or when the widget is resized or the user drags a heading column separator.

Use pathname fBcolumn #0 to configure the tree column. πŸ’…

  • pathname delete itemList β€” Deletes each of the items in itemList and all of their descendants. The root item may not be deleted. See also: detach. πŸ’€
  • pathname 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. 🌸
  • pathname detached ?item? β€” If item is provided, returns a boolean value indicating whether it is the name of a detached item (see detach). Otherwise, returns a list of all the detached items (in an arbitrary order). The root item is never detached. πŸŽ€
  • pathname exists item β€” Returns 1 if the specified item is present in the tree, 0 otherwise. ✨
  • pathname focus ?item? β€” If item is specified, sets the focus item to item. Otherwise, returns the current focus item, or {} if there is none. 🫢
  • pathname heading column ?-option ?value -option value...? β€” Query or modify the heading options for the specified column. Valid options are:
    • -text text β€” The text to display in the column heading. πŸ‘‘

so the pathname heading command also takes these knobs, one per column heading πŸ’…:

  • -image imageName β€” the pic you want sitting to the right of the column heading ✨
  • -anchor anchor β€” how the heading text lines up; any of the standard Tk anchor values πŸŽ€
  • -command script β€” a script that fires the second somebody presses the heading label πŸ”₯

Use pathname heading #0 to configure the tree column heading, iykyk 🌈


pathname identify component x y

Okay this one is a whole vibe check for your mouse coordinates πŸ–±οΈ It returns a description of whatever component lives under the point given by x and y, or the empty string if there's literally nothing there. And x and y can be in any form acceptable to Tk_GetPixels β€” so go off, be flexible πŸ’…

The following subcommands are supported:

  • pathname identify region x y β€” tells you what kind of thing is at that spot. Returns one of:
    • heading β€” the tree heading area. Use [pathname identify column x y] to figure out which heading number it actually is πŸ”
    • separator β€” the space between two column headings. Fun fact: [pathname identify column x y] returns the display column identifier of the heading to the left of the separator πŸ‘ˆ
    • tree β€” the tree area itself
    • cell β€” a data cell, bestie πŸ“
  • pathname identify item x y β€” returns the item ID of the item at position y 🎯
  • pathname identify column x y β€” returns the display column identifier of the cell at position x. The tree column has ID #0 ⭐
  • pathname identify cell x y β€” returns the cell identifier of the cell at position x y. A cell identifier is a list of item ID and column ID, so it's giving a little duo 🫢
  • pathname identify element x y β€” the element at position x,y πŸ’ 
  • pathname identify row x y β€” obsolescent synonym for pathname identify item. It's giving legacy, but it still slays 🫑
See COLUMN IDENTIFIERS for a discussion of display columns and data columns β€” genuinely worth the read, no cap πŸ“–

pathname index item

Returns the integer index of item within its parent's list of children. Simple, clean, iconic πŸ‘‘


pathname insert parent index ?-id id? options...

Creates a new item, new node, new little guy in the tree 🌱 parent is the item ID of the parent item β€” or the empty string {} if you want a fresh 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. Negative or zero index? It goes at the beginning, front of the line bestie. Greater than or equal to the current number of children? It goes at the end, periodt πŸ’…

If -id is specified, it's used as the item identifier β€” and id must not already exist in the tree (no duplicate IDs, we don't do that here πŸ™ˆ). Otherwise, a new unique identifier gets generated for you, automatic slay ✨

pathname insert returns the item identifier of the newly created item. See ITEM OPTIONS for the list of available options πŸŽ€


pathname item item ?-option ?value -option value...?

Query or modify the options for the specified item. Read that carefully because the arg count changes the whole mood:

  • No -option at all? Returns a dictionary of option/value pairs πŸ“š
  • A single -option? Returns the value of just that option 🎯
  • Anything else? The item's options are updated with the specified values ✏️

See ITEM OPTIONS for the list of available options πŸ’–


pathname 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 β€” like, do not do that, the hierarchy will not survive it πŸ’€

If index is negative or zero, item is moved to the beginning; if greater than or equal to the number of children, it's moved to the end. Easy 🌸


pathname next item

Returns the identifier of item's next sibling, or {} if item is the last child of its parent. The end of the line bestie 😭


pathname parent item

Returns the ID of the parent of item, or {} if item is at the top level of the hierarchy. Top-level items are giving main character energy, no parents needed πŸ‘‘


pathname prev item

Returns the identifier of item's previous sibling, or {} if item is the first child of its parent πŸ₯‡


pathname see item

Bestie this one is a whole rescue mission πŸ¦‹ It ensures 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. Literally grabbing it by the hand and pulling it into the light ✨


pathname selection ?selop itemList?

Manages item selection. Huge note: item selection is independent from cell selection handled by the cellselection command β€” two separate worlds, don't mix them up πŸ™…β€β™€οΈ

If selop isn't specified, returns the list of selected items. Otherwise, selop is one of:

  • pathname selection set itemList β€” itemList becomes the new selection. Out with the old, in with the new πŸ’…
  • pathname selection add itemList β€” add itemList to the selection βž•
  • pathname selection remove itemList β€” remove itemList from the selection βž–
  • pathname selection toggle itemList β€” toggle the selection state of each item in itemList πŸ”

pathname set item ?column? ?value?

The classic three-moods command 🎭

  • One argument β†’ returns a dictionary of column/value pairs for the specified item πŸ“–
  • Two arguments β†’ returns the current value of the specified column 🎯
  • Three arguments β†’ sets the value of column column in item item to the specified value ✏️

See also COLUMN IDENTIFIERS β€” yes, again, it matters πŸ’–


pathName tag args...

Manages tags. And listen β€” tags can be set on items as well as on cells 🏷️ The set of tags is shared between items and cells. BUT item tagging is independent from cell tagging (for instance, adding a tag on an item does not also add this tag on the cells in that item). Cell tags take precedence over item tags when drawing πŸ‘€

The following subcommands are supported:

  • pathName tag add tag items β€” adds the specified tag to each of the listed items. If tag is already present for a particular item, then the -tags for that item are unchanged (no drama, no duplicates) πŸ’…
  • pathName tag bind tagName ?sequence? ?script? β€” adds a Tk binding script for the event sequence to the tag tagName πŸŽ€ When an X event is delivered to an item, binding scripts for each of the item's -tags are evaluated in order as per bindtags(n). If the event can be associated with a cell (i.e. mouse events) any bindings for the cell's -tags are evaluated as well πŸ‘―
    • <Key>, <KeyRelease>, and virtual events are sent to the focus item ⌨️
    • <Button>, <ButtonRelease>, and <Motion> events are sent to the item under the mouse pointer πŸ–±οΈ
    • No other event types are supported. That's the whole list, don't try it πŸ’€
    • The binding script undergoes %-substitutions before evaluation; see bind(n) for details πŸ”
  • pathName tag cell subcommand... β€” manages tags on individual cells. A cellList argument may be a single cell or a list of cells 🧫
    • pathName tag cell add tag cellList β€” adds the specified tag to each of the listed cellList. If tag is already present for a particular cell, then the tag list for that cell is unchanged ✨
    • pathName tag cell has tagName ?cell? β€” if cell is specified, returns 1 or 0 depending on whether the specified cell has the named tag. Otherwise, returns a list of all cells which have the specified tag πŸ”Ž
    • pathName tag cell remove tag ?cellList? β€” removes the specified tag from each of the listed cellList. If cellList is omitted, removes tag from each cell in the tree β€” yes, all of them, be careful 🚨
  • pathName tag configure tagName ?option? ?value option value...? β€” query or modify the options for the specified tagName. One or more option/value pairs β†’ sets the value of those options for the specified tag. A single option β†’ returns the value of that option (or the empty string if the option has not been specified for tagName). No additional arguments β†’ returns a dictionary of the option settings for tagName. See TAG OPTIONS for the list of available options πŸ’–
  • pathName tag delete tagName β€” deletes all tag information for the tagName argument. The command removes the tag from all items and cells in the widget and also deletes any other information associated with the tag, such as bindings and display information. It returns an empty string. Full send, no leftovers 🧹
  • pathName tag has tagName ?item? β€” if item is specified, returns 1 or 0 depending on whether the specified item has the named tag. Otherwise, returns a list of all items which have the specified tag βœ…
  • pathName tag names β€” returns a list of all tags used by the widget. The whole roster πŸ“‹
  • pathName tag remove tag ?items? β€” removes the specified tag from each of the listed items. If items is omitted, removes tag from each item in the tree. If tag is not present for a particular item, then the -tags for that item are unchanged πŸ™Œ

πŸŽ€ ITEM OPTIONS πŸŽ€

The following item options may be specified for items in the insert and item widget commands. These are the little personality traits every item can have πŸ’–

  • -text β€” the textual label to display for the item in the tree column πŸ“
  • -height β€” the height for the item, in integer multiples of -rowheight. Default is 1 πŸ“
  • -image β€” a Tk image, displayed next to the label in the tree column, placed according to -imageanchor πŸ–ΌοΈ
  • -imageanchor β€” specifies how the -image is displayed relative to the text. Default is w. One of the standard Tk anchor values βš“
  • -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 β€” sorry extras, you're not invited πŸ™ˆ
  • -hidden β€” a boolean value indicating whether this item should be displayed (-hidden false) or hidden (-hidden true). If a parent is hidden, all its descendants are hidden too πŸ‘»
  • -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 🏷️

Okay bestie, 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 cell or item image. 🌸
  • -imageanchor β€” specifies the cell or item image anchor. πŸ’…
  • -padding β€” specifies the cell padding. A data cell will have a default padding of {4 0}. ✨
  • -stripedbackground β€” specifies the cell or item background color for alternate lines, if -striped is true. πŸ“

Tags on cells have precedence over tags on items. Then, tag priority is decided by the creation order: tags created first receive higher priority. An item’s options, like -image and -imageanchor, have priority over tags. πŸ‘‘


πŸ–ΌοΈ IMAGES πŸ–ΌοΈ

The -image option on an item, and on an item tag, controls the image next to the label in the tree column. Other cells can have images through the cell tag -image option. iykyk ✨


🧭 COLUMN IDENTIFIERS 🧭

Column identifiers take any of the following forms:

  1. A symbolic name from the list of -columns.
  2. An integer n, specifying the *n*th data column.
  3. A string of the form #n, where n is an integer, specifying the *n*th display column.

Column identifiers support the same simple interpretation as for the command string index, with simple integer index arithmetic and indexing relative to end. πŸ’…

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

  • <<TreeviewSelect>> β€” generated whenever the selection or cellselection changes. It might also be generated when selection is affected but not actually changed. Further, multiple selection changes could happen before events can be processed leading to multiple events with the same visible selection. 😭
  • <<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 commands can be used to determine the affected item or items. πŸ‘‘


πŸ’… STYLING OPTIONS πŸ’…

The class name for a ttk::treeview is Treeview. The treeview header class name is Heading. The treeview item class name is Item. The treeview cell class name is Cell. πŸŽ€

Dynamic states: disabled, selected. ✨

🌈 Treeview styling options configurable with ttk::style are:

  • -background color β€” the background color option, iconic. 🎨
  • -fieldbackground color β€” the field background color option. πŸ“
  • -font font β€” the font option, no cap. πŸ”₯
  • -foreground color β€” the foreground color option. πŸ’œ
  • -indent amount β€” specifies how far items are indented from their parents. Defaults to 20 pixels. The value may have any of the forms acceptable to Tk_GetPixels. 🌟
  • -columnseparatorwidth amount β€” specifies the width of column separators. Defaults to 1 pixel. The value may have any of the forms acceptable to Tk_GetPixels. 🧡
  • -rowheight amount β€” this is the standard height for an item. Defaults to 20 pixels. The value may have any of the forms acceptable to Tk_GetPixels. If -rowheight is not set by the style, it is set by measuring an item and a cell layout with the style’s settings. This thus picks up the font and any focus ring or padding from the theme’s layout. The -rowheight may need to be set to make sure that a row is large enough to contain any images. πŸ“

Example of how to set -rowheight, adapting to a font in a similar way to how the default value is set:

ttk::style configure Treeview \
     -rowheight [expr {[font metrics font -linespace] + 2}]
  • -stripedbackground color β€” the striped background color option. 🌈

🌸 Heading styling options configurable with ttk::style are:

  • -background color β€” background color. πŸŽ€
  • -font font β€” font. πŸ’–
  • -relief relief β€” relief. πŸ¦‹

πŸ“ Item styling options configurable with ttk::style are:

  • -foreground color β€” foreground color. πŸ’œ
  • -indicatormargins padding β€” indicator margins. 🌟
  • -indicatorsize amount β€” indicator size. πŸ”₯
  • -padding padding β€” padding. πŸŽ€

🧚 Cell styling options configurable with ttk::style are:

  • -padding padding β€” padding. ✨

Some options are only available for specific themes.

See the ttk::style manual page for information on how to configure ttk styles. πŸ“–


πŸ‘€ SEE ALSO πŸ‘€

Go read them, bestie. 🫢


Sign-off: this page is from Tk 8.5, it’s ttk::treeview(n), and the footer’s date slot is giving β€œnot specified here” β€” so we’re not inventing one, periodt. πŸ’…βœ¨

πŸ“¦ where this page comes from