|
| 1 | +# Tree |
| 2 | + |
| 3 | +A tree control widget. |
| 4 | + |
| 5 | +- [x] Focusable |
| 6 | +- [ ] Container |
| 7 | + |
| 8 | + |
| 9 | +## Example |
| 10 | + |
| 11 | +The example below creates a simple tree. |
| 12 | + |
| 13 | +=== "Output" |
| 14 | + |
| 15 | + ```{.textual path="docs/examples/widgets/tree.py"} |
| 16 | + ``` |
| 17 | + |
| 18 | +=== "tree.py" |
| 19 | + |
| 20 | + ```python |
| 21 | + --8<-- "docs/examples/widgets/tree.py" |
| 22 | + ``` |
| 23 | + |
| 24 | +A each tree widget has a "root" attribute which is an instance of a [TreeNode][textual.widgets.TreeNode]. Call [add()][textual.widgets.TreeNode.add] or [add_leaf()][textual.widgets.TreeNode.add_leaf] to add new nodes underneath the root. Both these methods return a TreeNode for the child, so you can add more levels. |
| 25 | + |
| 26 | + |
| 27 | +## Reactive Attributes |
| 28 | + |
| 29 | +| Name | Type | Default | Description | |
| 30 | +| ------------- | ------ | ------- | ----------------------------------------------- | |
| 31 | +| `show_root` | `bool` | `True` | Show the root node. | |
| 32 | +| `show_guides` | `bool` | `True` | Show guide lines between levels. | |
| 33 | +| `guide_depth` | `int` | `4` | Amount of indentation between parent and child. | |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +## Messages |
| 38 | + |
| 39 | +### NodeSelected |
| 40 | + |
| 41 | +The `Tree.NodeSelected` message is sent when the user selects a tree node. |
| 42 | + |
| 43 | + |
| 44 | +#### Attributes |
| 45 | + |
| 46 | +| attribute | type | purpose | |
| 47 | +| --------- | ------------------------------------ | -------------- | |
| 48 | +| `node` | [TreeNode][textual.widgets.TreeNode] | Selected node. | |
| 49 | + |
| 50 | + |
| 51 | +### NodeExpanded |
| 52 | + |
| 53 | +The `Tree.NodeExpanded` message is sent when the user expands a node in the tree. |
| 54 | + |
| 55 | +#### Attributes |
| 56 | + |
| 57 | +| attribute | type | purpose | |
| 58 | +| --------- | ------------------------------------ | -------------- | |
| 59 | +| `node` | [TreeNode][textual.widgets.TreeNode] | Expanded node. | |
| 60 | + |
| 61 | + |
| 62 | +### NodeCollapsed |
| 63 | + |
| 64 | + |
| 65 | +The `Tree.NodeCollapsed` message is sent when the user expands a node in the tree. |
| 66 | + |
| 67 | + |
| 68 | +#### Attributes |
| 69 | + |
| 70 | +| attribute | type | purpose | |
| 71 | +| --------- | ------------------------------------ | --------------- | |
| 72 | +| `node` | [TreeNode][textual.widgets.TreeNode] | Collapsed node. | |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +## See Also |
| 78 | + |
| 79 | +* [Tree][textual.widgets.Tree] code reference |
| 80 | +* [TreeNode][textual.widgets.TreeNode] code reference |
0 commit comments