Skip to content

Tree

A tree control widget.

  • Focusable
  • Container

Example

The example below creates a simple tree.

TreeApp ▼ Dune └── ▼ Characters     ├── Paul     ├── Jessica     └── Channi

from textual.app import App, ComposeResult
from textual.widgets import Tree


class TreeApp(App):
    def compose(self) -> ComposeResult:
        tree: Tree = Tree("Dune")
        tree.root.expand()
        characters = tree.root.add("Characters", expand=True)
        characters.add_leaf("Paul")
        characters.add_leaf("Jessica")
        characters.add_leaf("Channi")
        yield tree


if __name__ == "__main__":
    app = TreeApp()
    app.run()

A each tree widget has a "root" attribute which is an instance of a TreeNode. Call add() or add_leaf() to add new nodes underneath the root. Both these methods return a TreeNode for the child, so you can add more levels.

Reactive Attributes

Name Type Default Description
show_root bool True Show the root node.
show_guides bool True Show guide lines between levels.
guide_depth int 4 Amount of indentation between parent and child.

Messages

NodeSelected

The Tree.NodeSelected message is sent when the user selects a tree node.

Attributes

attribute type purpose
node TreeNode Selected node.

NodeExpanded

The Tree.NodeExpanded message is sent when the user expands a node in the tree.

Attributes

attribute type purpose
node TreeNode Expanded node.

NodeCollapsed

The Tree.NodeCollapsed message is sent when the user expands a node in the tree.

Attributes

attribute type purpose
node TreeNode Collapsed node.

See Also