ttk::treeview(3tk)
π okay bestie, sit down, grab a snack, hydrate β we're reading ttk::treeview(3tk) together, straight from the Tk Themed Widget corner of the manual. It's long so it comes in three parts, and this is part one. No skipping, I promise it's good. π
β¨ NAME β¨
ttk::treeview β a hierarchical multicolumn data display widget. It's giving tree, it's giving spreadsheet, it's giving both at once. π³
πΈ SYNOPSIS πΈ
ttk::treeview pathname ?options?
π¦ DESCRIPTION π¦
So the ttk::treeview widget displays a whole hierarchical collection of items. Each item has a textual label, an optional image, and an optional list of data values. Those data values get displayed in successive columns, right after the tree label. π
The order in which those data values show up can 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 β peep COLUMN IDENTIFIERS for that. π
Every item is identified by a unique name. If you don't hand the widget an item ID, it will generate one for you, no stress. There's one distinguished root item, and its name is {}. The root item itself is not displayed β its children are what show up at the top level of the hierarchy. π«Ά
Each item also has a list of tags, which you can use to associate event bindings with individual items and to control how the item looks. π
Treeview widgets support horizontal and vertical scrolling with the standard -[xy]scrollcommand options and the [xy]view widget commands. Scrolling? Slayed.
π STANDARD OPTIONS π
-class-cursor-takefocus-style-xscrollcommand-yscrollcommand-padding
See the ttk_widget manual entry for details on the standard options β I'm not re-teaching that class, bestie, that's a different episode. π
π WIDGET-SPECIFIC OPTIONS π
-columnsβ (database namecolumns, database classColumns) A list of column identifiers, specifying the number of columns and their names.-displaycolumnsβ (database namedisplayColumns, database classDisplayColumns) 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 it's set to#all(the default), all columns are shown, in the order given. β¨-heightβ (database nameheight, database classHeight) Specifies the number of rows which should be visible. Lowkey important: the requested width is determined from the sum of the column widths.-selectmodeβ (database nameselectMode, database classSelectMode) 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. So yeah, you can absolutely do your own thing here. π
- If set to
-selecttypeβ (database nameselectType, database classSelectType) Controls how the built-in class bindings manage the selection. One ofitemorcell.-showβ (database nameshow, database classShow) 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#0always refers to the tree column, even if-show treeis not specified. Always. No cap.
-stripedβ (database namestriped, database classStriped) Boolean specifying zebra striped item coloring. Note that striped items uses the-stripedbackgroundoption if set by the theme or a tag. If it's not supported by the current theme, it will not show. π¦-titlecolumnsβ (database nametitleColumns, database classTitleColumns) Number of display columns at the left that should not be scrolled. The tree column counts, even if-show treeis not specified. So for value N of this option, column#Nis the first one that is scrollable. Default is0.-titleitemsβ (database nametitleItems, database classTitleItems) Number of items at the top that should not be vertically scrolled. Default is0.
π WIDGET COMMAND π
On top of the standard cget, configure, instate, state, style, xview and yview commands (see ttk::widget for those), treeview widgets support all of the following extra commands. Yes, there are a lot. We're doing them all. π
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 β like, if it's a descendant of a closed item, or it's been vertically scrolled offscreen β it returns the empty list. If column is specified and is not hidden (by the-displaycolumnsoption), it returns the bounding box of that cell within item, even if that cell is horizontally scrolled offscreen. πpathname cellselection ?selop arg ...?β Manages cell selection. Cell selection is independent from item selection, which is handled by theselectioncommand. A cell is given by a list of two elements, item and column. For the rectangle versions of the commands, the cells must be in displayed columns. Any change to-columnsclears the cell selection. A cellList argument may be a single cell or a list of cells. If selop is not specified, it 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, it replaces item's child list with newchildren. Items in the old child list that aren't present in the new child list get detached from the tree. And heads up: 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 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 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 column #0to 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 themoveoperation, but they 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 (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. Vibe check: passed or not passed, that's it.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.
π pathname heading β the heading boss fight
-imageimageName β drops an image right to the side of the column heading. cute lil visual moment β¨-anchoranchor β how the heading text lines up. one of the standard Tk anchor values, bestie, pick your fave π-commandscript β a script that runs when somebody presses the heading label. click = action, periodt π₯
Use pathname heading #0 to configure the tree column heading. yes, the tree column has a heading too, she's not exempt π«Ά
π pathname 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. The values x and y may have any of the forms acceptable to Tk_GetPixels. The following subcommands are supported:
pathname identify region x y
Returns one of:
headingβ Tree heading area; use[pathname identify column x y]to determine the heading number.separatorβ Space between two column headings;[pathname identify column x y]will return the display column identifier of the heading to left of the separator.treeβ The tree area.cellβ A data cell.
pathname identify item x y
Returns the item ID of the item at position y. lowkey just "who lives here" π
pathname identify column x y
Returns the display column identifier of the cell at position x. The tree column has ID #0. she's always number zero, iconic behavior π
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.
pathname identify element x y
The element at position x,y.
pathname identify row x y
Obsolescent synonym for pathname identify item. old but still showing up to the party π
See COLUMN IDENTIFIERS for a discussion of display columns and data columns.
π’ pathname index item
Returns the integer index of item within its parent's list of children. basically "what number child are you" and the answer is a number, no drama π
π± pathname insert parent index ?-id id? options...
Creates a new 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 negative or 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 -id is specified, it is used as the item identifier; id must not already exist in the tree. Otherwise, a new unique identifier is generated.
pathname insert returns the item identifier of the newly created item. See ITEM OPTIONS for the list of available options. new item just dropped π
β¨
π οΈ pathname item item ?-option ?value -option value...?
Query or modify the options for the specified item. 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 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. bestie do NOT do that, it's a family tree not a family pretzel π
If index is negative or zero, item is moved to the beginning; if greater than or equal to the number of children, it is moved to the end.
β‘οΈ pathname next item
Returns the identifier of item's next sibling, or {} if item is the last child of its parent.
π pathname parent item
Returns the ID of the parent of item, or {} if item is at the top level of the hierarchy.
β¬
οΈ pathname prev item
Returns the identifier of item's previous sibling, or {} if item is the first child of its parent.
π pathname see item
Ensure that 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. it's giving "main character, everyone look at her" π
π pathname selection ?selop itemList?
Manages item selection. Item selection is independent from cell selection handled by the cellselection command. If selop is not specified, returns the list of selected items. Otherwise, selop is one of the following:
pathname selection set itemListβ itemList becomes the new selection.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?
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 column column in item item to the specified value. See also COLUMN IDENTIFIERS.
π·οΈ pathName tag args...
Manages tags. Tags can be set on items as well as on cells. The set of tags is shared between items and cells. However 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.pathName tag bind tagName ?sequence? ?script?β Add a Tk binding script for the event sequence 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(3tk). 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.
The binding script undergoes %-substitutions before evaluation; see bind(3tk) for details. fr fr, check the other page π
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.
pathName tag configure tagName ?option? ?value option value...?β Query or modify the options for the specified tagName. If one or more option/value pairs are specified, sets the value of those options for the specified tag. If a single option is specified, returns the value of that option (or the empty string if the option has not been specified for tagName). With 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. The command returns an empty string. full send goodbye ππ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.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.
-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 -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.
-hiddenβ A boolean value indicating whether this item should be displayed (-hidden false) or hidden (-hidden true). If a parent is hidden, all its decendants are hidden too. sneaky little family-wide vanishing act π«₯-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, so tags have their own lil option menu and here's the whole thing, no skips:
-foregroundβ the color of the text. yes, the letters themselves. π-backgroundβ the background color of the cell or the item.-fontβ the font used when drawing text.-imageβ the image for the cell or the item.-imageanchorβ where that image gets anchored on the cell or item.-paddingβ the cell padding. a data cell lowkey comes with a default padding of{4 0}.-stripedbackgroundβ the cell or item background color for alternate lines, but ONLY if-stripedis true. zebra mode activated π¦
Now the hierarchy tea β: tags on cells have precedence over tags on items. Then tag priority is decided by creation order β tags created first get higher priority. and an item's own options, like -image and -imageanchor, have priority over tags. so item options > tag priority > nothing, basically. periodt. π
πΌοΈ 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 too, through the cell tag -image option. so yes bestie, images in cells, slay. β¨
π’ COLUMN IDENTIFIERS
Column identifiers can be any of these 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. iykyk. π§
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. it's giving main character energy, always present π
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. no cap. π«Ά
β‘ VIRTUAL EVENTS
The treeview widget generates the following virtual events, and they're all kinda iconic:
<<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. so yeah, it's a lil extra sometimes π<<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. go check, bestie π
π¨ 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. four classes, four vibes. π
Dynamic states: disabled, selected.
Treeview styling options configurable with ttk::style are:
-backgroundcolor-fieldbackgroundcolor-fontfont-foregroundcolor-indentamount β Specifies how far items are indented from their parents. Defaults to 20 pixels. The value may have any of the forms acceptable toTk_GetPixels.-columnseparatorwidthamount β Specifies the width of column separators. Defaults to 1 pixel. The value may have any of the forms acceptable toTk_GetPixels.-rowheightamount β This is the standard height for an item. Defaults to 20 pixels. 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. π-stripedbackgroundcolor
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}]
Heading styling options configurable with ttk::style are:
-backgroundcolor-fontfont-reliefrelief
Item styling options configurable with ttk::style are:
-foregroundcolor-indicatormarginspadding-indicatorsizeamount-paddingpadding
Cell styling options configurable with ttk::style are:
-paddingpadding
Some options are only available for specific themes. so if something's not working, check your theme first, bestie π
See the ttk::style manual page for information on how to configure ttk styles. that's your homework π
π SEE ALSO
ttk::widget(3tk)listbox(3tk)image(3tk)bind(3tk)
and that's the whole page, you made it to the end bestie, I'm so proud of you π₯Ήπ this one comes from Tk 8.5, so go forth and make the most gorgeous treeview the world has ever seen. you're literally the main character of Tk now. no cap. mwah π β¨πΈ
π¦ where this page comes from
- debian trixie: tk9.0-doc 9.0.1-3
- ubuntu questing: tk9.0-doc 9.0.2-1
- ubuntu resolute: tk9.0-doc 9.0.3-1
- ubuntu stonking: tk9.0-doc 9.0.4-1
- Copyright (c) 2004 Joe English
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms