Tree¶
Added in version 0.6.0
A tree control widget.
- Focusable
- Container
Example¶
The example below creates a simple tree.
from textual.app import App, ComposeResult
from textual.widgets import Tree
class TreeApp(App):
def compose(self) -> ComposeResult:
tree: Tree[str] = Tree("Dune")
tree.root.expand()
characters = tree.root.add("Characters", expand=True)
characters.add_leaf("Paul")
characters.add_leaf("Jessica")
characters.add_leaf("Chani")
yield tree
if __name__ == "__main__":
app = TreeApp()
app.run()
Tree widgets have 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 which you can use to add additional 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¶
Bindings¶
The tree widget defines the following bindings:
Key(s) | Description |
---|---|
enter | Select the current item. |
space | Toggle the expand/collapsed space of the current item. |
up | Move the cursor up. |
down | Move the cursor down. |
Component Classes¶
The tree widget provides the following component classes:
Class | Description |
---|---|
tree--cursor |
Targets the cursor. |
tree--guides |
Targets the indentation guides. |
tree--guides-hover |
Targets the indentation guides under the cursor. |
tree--guides-selected |
Targets the indentation guides that are selected. |
tree--highlight |
Targets the highlighted items. |
tree--highlight-line |
Targets the lines under the cursor. |
tree--label |
Targets the (text) labels of the items. |
Bases: Generic[TreeDataType]
, ScrollView
A widget for displaying and navigating data in a tree.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
TextType
|
The label of the root node of the tree. |
required |
|
TreeDataType | None
|
The optional data to associate with the root node of the tree. |
None
|
|
str | None
|
The name of the Tree. |
None
|
|
str | None
|
The ID of the tree in the DOM. |
None
|
|
str | None
|
The CSS classes of the tree. |
None
|
|
bool
|
Whether the tree is disabled or not. |
False
|
BINDINGS
class-attribute
¶
BINDINGS = [
Binding(
"shift+left",
"cursor_parent",
"Cursor to parent",
show=False,
),
Binding(
"shift+right",
"cursor_parent_next_sibling",
"Cursor to next ancestor",
show=False,
),
Binding(
"shift+up",
"cursor_previous_sibling",
"Cursor to previous sibling",
show=False,
),
Binding(
"shift+down",
"cursor_next_sibling",
"Cursor to next sibling",
show=False,
),
Binding("enter", "select_cursor", "Select", show=False),
Binding("space", "toggle_node", "Toggle", show=False),
Binding(
"shift+space",
"toggle_expand_all",
"Expand or collapse all",
show=False,
),
Binding("up", "cursor_up", "Cursor Up", show=False),
Binding(
"down", "cursor_down", "Cursor Down", show=False
),
]
Key(s) | Description |
---|---|
enter | Select the current item. |
space | Toggle the expand/collapsed space of the current item. |
up | Move the cursor up. |
down | Move the cursor down. |
COMPONENT_CLASSES
class-attribute
¶
COMPONENT_CLASSES = {
"tree--cursor",
"tree--guides",
"tree--guides-hover",
"tree--guides-selected",
"tree--highlight",
"tree--highlight-line",
"tree--label",
}
Class | Description |
---|---|
tree--cursor |
Targets the cursor. |
tree--guides |
Targets the indentation guides. |
tree--guides-hover |
Targets the indentation guides under the cursor. |
tree--guides-selected |
Targets the indentation guides that are selected. |
tree--highlight |
Targets the highlighted items. |
tree--highlight-line |
Targets the lines under the cursor. |
tree--label |
Targets the (text) labels of the items. |
ICON_NODE
class-attribute
instance-attribute
¶
Unicode 'icon' to use for an expandable node.
ICON_NODE_EXPANDED
class-attribute
instance-attribute
¶
Unicode 'icon' to use for an expanded node.
auto_expand
class-attribute
instance-attribute
¶
auto_expand = var(True)
Auto expand tree nodes when they are selected.
center_scroll
class-attribute
instance-attribute
¶
center_scroll = var(False)
Keep selected node in the center of the control, where possible.
cursor_line
class-attribute
instance-attribute
¶
cursor_line = var(-1, always_update=True)
The line with the cursor, or -1 if no cursor.
guide_depth
class-attribute
instance-attribute
¶
The indent depth of tree nodes.
hover_line
class-attribute
instance-attribute
¶
hover_line = var(-1)
The line number under the mouse pointer, or -1 if not under the mouse pointer.
show_guides
class-attribute
instance-attribute
¶
show_guides = reactive(True)
Enable display of tree guide lines.
show_root
class-attribute
instance-attribute
¶
show_root = reactive(True)
Show the root of the tree.
NodeCollapsed
¶
Bases: Generic[EventTreeDataType]
, Message
Event sent when a node is collapsed.
Can be handled using on_tree_node_collapsed
in a subclass of Tree
or in a
parent node in the DOM.
NodeExpanded
¶
Bases: Generic[EventTreeDataType]
, Message
Event sent when a node is expanded.
Can be handled using on_tree_node_expanded
in a subclass of Tree
or in a
parent node in the DOM.
NodeHighlighted
¶
Bases: Generic[EventTreeDataType]
, Message
Event sent when a node is highlighted.
Can be handled using on_tree_node_highlighted
in a subclass of Tree
or in a
parent node in the DOM.
NodeSelected
¶
Bases: Generic[EventTreeDataType]
, Message
Event sent when a node is selected.
Can be handled using on_tree_node_selected
in a subclass of Tree
or in a
parent node in the DOM.
action_cursor_next_sibling
¶
Move the cursor to the next sibling, or to the paren't sibling if there are no more siblings.
action_cursor_parent_next_sibling
¶
Move the cursor to the parent's next sibling.
action_cursor_previous_sibling
¶
Move the cursor to previous sibling, or to the parent if there are no more siblings.
action_scroll_end
¶
Move the cursor to the bottom of the tree.
Note
Here bottom means vertically, not branch depth.
action_select_cursor
¶
Cause a select event for the target node.
Note
If auto_expand
is True
use of this action on a non-leaf node
will cause both an expand/collapse event to occur, as well as a
selected event.
action_toggle_expand_all
¶
Expand or collapse all siblings.
If all the siblings are collapsed then they will be expanded. Otherwise they will all be collapsed.
add_json
¶
get_label_width
¶
get_label_width(node)
Get the width of the nodes label.
The default behavior is to call render_label
and return the cell length. This method may be
overridden in a sub-class if it can be done more efficiently.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
TreeNode[TreeDataType]
|
A node. |
required |
Returns:
Type | Description |
---|---|
int
|
Width in cells. |
get_node_at_line
¶
get_node_at_line(line_no)
Get the node for a given line.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
int
|
A line number. |
required |
Returns:
Type | Description |
---|---|
TreeNode[TreeDataType] | None
|
A tree node, or |
get_node_by_id
¶
get_node_by_id(node_id)
Get a tree node by its ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
NodeID
|
The ID of the node to get. |
required |
Returns:
Type | Description |
---|---|
TreeNode[TreeDataType]
|
The node associated with that ID. |
Raises:
Type | Description |
---|---|
UnknownNodeID
|
Raised if the |
move_cursor
¶
Move the cursor to the given node, or reset cursor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
TreeNode[TreeDataType] | None
|
A tree node, or None to reset cursor. |
required |
|
bool
|
Enable animation |
False
|
move_cursor_to_line
¶
Move the cursor to the given line.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
int
|
The line number (negative indexes are offsets from the last line). |
required |
|
Enable scrolling animation. |
False
|
Raises:
Type | Description |
---|---|
IndexError
|
If the line doesn't exist. |
render_label
¶
render_label(node, base_style, style)
Render a label for the given node. Override this to modify how labels are rendered.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
TreeNode[TreeDataType]
|
A tree node. |
required |
|
Style
|
The base style of the widget. |
required |
|
Style
|
The additional style for the label. |
required |
Returns:
Type | Description |
---|---|
Text
|
A Rich Text object containing the label. |
reset
¶
Clear the tree and reset the root node.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
TextType
|
The label for the root node. |
required |
|
TreeDataType | None
|
Optional data for the root node. |
None
|
Returns:
Type | Description |
---|---|
Self
|
The |
scroll_to_line
¶
scroll_to_node
¶
Scroll to the given node.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
TreeNode[TreeDataType]
|
Node to scroll in to view. |
required |
|
bool
|
Animate scrolling. |
True
|
select_node
¶
select_node(node)
Move the cursor to the given node and select it, or reset cursor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
TreeNode[TreeDataType] | None
|
A tree node to move the cursor to and select, or None to reset cursor. |
required |
Make non-widget Tree support classes available.
EventTreeDataType
module-attribute
¶
EventTreeDataType = TypeVar('EventTreeDataType')
The type of the data for a given instance of a Tree.
Similar to TreeDataType but used for
Tree
messages.
TreeDataType
module-attribute
¶
TreeDataType = TypeVar('TreeDataType')
The type of the data for a given instance of a Tree.
AddNodeError
¶
Bases: Exception
Exception raised when there is an error with a request to add a node.
TreeNode
¶
Bases: Generic[TreeDataType]
An object that represents a "node" in a tree control.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Tree[TreeDataType]
|
The tree that the node is being attached to. |
required |
|
TreeNode[TreeDataType] | None
|
The parent node that this node is being attached to. |
required |
|
NodeID
|
The ID of the node. |
required |
|
Text
|
The label for the node. |
required |
|
TreeDataType | None
|
Optional data to associate with the node. |
None
|
|
bool
|
Should the node be attached in an expanded state? |
True
|
|
bool
|
Should the node allow being expanded by the user? |
True
|
add
¶
Add a node to the sub-tree.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
TextType
|
The new node's label. |
required |
|
TreeDataType | None
|
Data associated with the new node. |
None
|
|
int | TreeNode[TreeDataType] | None
|
Optional index or |
None
|
|
int | TreeNode[TreeDataType] | None
|
Optional index or |
None
|
|
bool
|
Node should be expanded. |
False
|
|
bool
|
Allow use to expand the node via keyboard or mouse. |
True
|
Returns:
Type | Description |
---|---|
TreeNode[TreeDataType]
|
A new Tree node |
Raises:
Type | Description |
---|---|
AddNodeError
|
If there is a problem with the addition request. |
Note
Only one of before
or after
can be provided. If both are
provided a AddNodeError
will be raised.
add_leaf
¶
Add a 'leaf' node (a node that can not expand).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
TextType
|
Label for the node. |
required |
|
TreeDataType | None
|
Optional data. |
None
|
|
int | TreeNode[TreeDataType] | None
|
Optional index or |
None
|
|
int | TreeNode[TreeDataType] | None
|
Optional index or |
None
|
Returns:
Type | Description |
---|---|
TreeNode[TreeDataType]
|
New node. |
Raises:
Type | Description |
---|---|
AddNodeError
|
If there is a problem with the addition request. |
Note
Only one of before
or after
can be provided. If both are
provided a AddNodeError
will be raised.
collapse
¶
Collapse the node (hide its children).
Returns:
Type | Description |
---|---|
Self
|
The |
collapse_all
¶
Collapse the node (hide its children) and all those below it.
Returns:
Type | Description |
---|---|
Self
|
The |
expand
¶
Expand the node (show its children).
Returns:
Type | Description |
---|---|
Self
|
The |
expand_all
¶
Expand the node (show its children) and all those below it.
Returns:
Type | Description |
---|---|
Self
|
The |
remove
¶
Remove this node from the tree.
Raises:
Type | Description |
---|---|
RemoveRootError
|
If there is an attempt to remove the root. |
set_label
¶
set_label(label)
Set a new label for the node.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
TextType
|
A |
required |
toggle
¶
Toggle the node's expanded state.
Returns:
Type | Description |
---|---|
Self
|
The |
toggle_all
¶
Toggle the node's expanded state and make all those below it match.
Returns:
Type | Description |
---|---|
Self
|
The |