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 thettk_widgetmanual entry for details.-cursorβ standard option. See thettk_widgetmanual entry for details.-takefocusβ standard option. See thettk_widgetmanual entry for details.-styleβ standard option. See thettk_widgetmanual entry for details.-xscrollcommandβ standard option. See thettk_widgetmanual entry for details.-yscrollcommandβ standard option. See thettk_widgetmanual entry for details.-paddingβ standard option. See thettk_widgetmanual 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 ofextended,browse, ornone.
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 ofitemorcell. πΈ-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-stripedbackgroundoption 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 treeis not specified. Thus for value N of this option, column#Nis the first one that is scrollable. Default is0. π-titleitemsβ Command-Line Name:-titleitems; Database Name:titleItems; Database Class:TitleItems. Number of items at the top that should not be vertically scrolled. Default is0. β¨
π₯ 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 formx 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. Ifcolumnis specified and is not hidden (by the-displaycolumnsoption), returns the bounding box of that cell withinitem(even if the cell is horizontally scrolled offscreen). πpathname cellselection ?selop arg ...?β Manages cell selection. Cell selection is independent from item selection handled by theselectioncommand. A cell is given by a list of two elements,itemandcolumn. For the rectangle versions of commands, the cells must be in displayed columns. Any change to-columnsclears the cell selection. AcellListargument may be a single cell or a list of cells. Ifselopis not specified, returns the list of selected cells. Otherwise,selopis one of the following:pathname cellselection set cellListβcellListbecomes the new cell selection.pathname cellselection set firstCell lastCellβ The rectangle defined becomes the new cell selection.pathname cellselection add cellListβ AddcellListto the cell selection.pathname cellselection add firstCell lastCellβ The rectangle defined is added to the cell selection.pathname cellselection remove cellListβ RemovecellListfrom 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 incellList. 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?β Ifnewchildrenis not specified, returns the list of children belonging toitem.
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-optionis specified, returns a dictionary of option/value pairs. If a single-optionis 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 columnn. 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 ofn,ne,e,se,s,sw,w,nw, orcenter.-minwidth minwidthβ The minimum width of the column in pixels. The treeview widget will not make the column any smaller than-minwidthwhen the widget is resized or the user drags a heading column separator. Default is20pixels.-separator booleanβ Specifies whether or not a column separator should be drawn to the right of the column. Default isfalse.-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 byTcl_GetBoolean. By default columns are stretchable.-width widthβ The width of the column in pixels. Default is200pixels. The specified column width may be changed by Tk in order to honor-stretchand/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 initemListand all of their descendants. The root item may not be deleted. See also:detach. πpathname detach itemListβ Unlinks all of the specified items initemListfrom the tree. The items and all of their descendants are still present and may be reinserted at another point in the tree with themoveoperation, but will not be displayed until that is done. The root item may not be detached. See also:delete. πΈpathname detached ?item?β Ifitemis provided, returns a boolean value indicating whether it is the name of a detached item (seedetach). Otherwise, returns a list of all the detached items (in an arbitrary order). The root item is never detached. πpathname exists itemβ Returns1if the specified item is present in the tree,0otherwise. β¨pathname focus ?item?β Ifitemis specified, sets the focus item toitem. 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 π
:
-imageimageName β the pic you want sitting to the right of the column heading β¨-anchoranchor β how the heading text lines up; any of the standard Tk anchor values π-commandscript β 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 itselfcellβ 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 forpathname 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
-optionat 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-tagsfor 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-tagsare evaluated in order as perbindtags(n). If the event can be associated with a cell (i.e. mouse events) any bindings for the cell's-tagsare 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; seebind(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, returns1or0depending 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, returns1or0depending 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-tagsfor 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 is1π-imageβ a Tk image, displayed next to the label in the tree column, placed according to-imageanchorπΌοΈ-imageanchorβ specifies how the-imageis displayed relative to the text. Default isw. 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-columnswidget 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-stripedis 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:
- A symbolic name from the list of
-columns. - An integer n, specifying the *n*th data column.
- 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#0always refers to the tree column, even if-show treeis 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:
-backgroundcolor β the background color option, iconic. π¨-fieldbackgroundcolor β the field background color option. π-fontfont β the font option, no cap. π₯-foregroundcolor β the foreground color option. π-indentamount β specifies how far items are indented from their parents. Defaults to20pixels. The value may have any of the forms acceptable toTk_GetPixels. π-columnseparatorwidthamount β specifies the width of column separators. Defaults to1pixel. The value may have any of the forms acceptable toTk_GetPixels. π§΅-rowheightamount β this is the standard height for an item. Defaults to20pixels. The value may have any of the forms acceptable toTk_GetPixels. If-rowheightis 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-rowheightmay 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}]
-stripedbackgroundcolor β the striped background color option. π
πΈ Heading styling options configurable with ttk::style are:
-backgroundcolor β background color. π-fontfont β font. π-reliefrelief β relief. π¦
π Item styling options configurable with ttk::style are:
-foregroundcolor β foreground color. π-indicatormarginspadding β indicator margins. π-indicatorsizeamount β indicator size. π₯-paddingpadding β padding. π
π§ Cell styling options configurable with ttk::style are:
-paddingpadding β 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 π
ttk::widget(n)listbox(n)image(n)bind(n)
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
- alpine 3.23: tk9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tk9-doc 9.0.3-r2 Β· TCL
- fedora 43: tk 1:9.0.2-1.fc43 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- fedora 43: tk 1:9.0.2-1.fc43 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- fedora 44: tk 1:9.0.2-1.fc44 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- fedora 44: tk 1:9.0.2-1.fc44 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- nixos 26.05: tk 9.0.3
- Copyright (c) 2004 Joe English
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms