Skip to content

Tree

Tree

Bases: Generic[TreeDataType], ScrollView

auto_expand = var(True) class-attribute

cursor_line = var(-1) class-attribute

cursor_node: TreeNode[TreeDataType] | None property

TreeNode | Node: The currently selected node, or None if no selection.

guide_depth = reactive(4, init=False) class-attribute

hover_line = var(-1) class-attribute

last_line: int property

show_guides = reactive(True) class-attribute

show_root = reactive(True) class-attribute

NodeCollapsed

Bases: Generic[EventTreeDataType], Message

Event sent when a node is collapsed.

Attributes:

Name Type Description
node TreeNode[EventTreeDataType]

The node that was collapsed.

NodeExpanded

Bases: Generic[EventTreeDataType], Message

Event sent when a node is expanded.

Attributes:

Name Type Description
node TreeNode[EventTreeDataType]

The node that was expanded.

NodeHighlighted

Bases: Generic[EventTreeDataType], Message

Event sent when a node is highlighted.

Attributes:

Name Type Description
node TreeNode[EventTreeDataType]

The node that was highlighted.

NodeSelected

Bases: Generic[EventTreeDataType], Message

Event sent when a node is selected.

Attributes:

Name Type Description
node TreeNode[EventTreeDataType]

The node that was selected.

UnknownNodeID

Bases: Exception

Exception raised when referring to an unknown TreeNode ID.

clear()

Clear all nodes under root.

get_label_width(node)

Get the width of the nodes label.

The default behavior is to call render_node 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
node TreeNode[TreeDataType]

A node.

required

Returns:

Type Description
int

Width in cells.

get_node_at_line(line_no)

Get the node for a given line.

Parameters:

Name Type Description Default
line_no int

A line number.

required

Returns:

Type Description
TreeNode[TreeDataType] | None

A tree node, or None if there is no node at that line.

get_node_by_id(node_id)

Get a tree node by its ID.

Parameters:

Name Type Description Default
node_id NodeID

The ID of the node to get.

required

Returns:

Type Description
TreeNode[TreeDataType]

The node associated with that ID.

Raises:

Type Description
Tree.UnknownID

Raised if the TreeNode ID is unknown.

process_label(label)

Process a str or Text in to a label. Maybe overridden in a subclass to change modify how labels are rendered.

Parameters:

Name Type Description Default
label TextType

Label.

required

Returns:

Type Description

A Rich Text object.

refresh_line(line)

Refresh (repaint) a given line in the tree.

Parameters:

Name Type Description Default
line int

Line number.

required

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
node TreeNode[TreeDataType]

A tree node.

required
base_style Style

The base style of the widget.

required
style Style

The additional style for the label.

required

Returns:

Type Description
Text

A Rich Text object containing the label.

scroll_to_line(line)

Scroll to the given line.

Parameters:

Name Type Description Default
line int

A line number.

required

scroll_to_node(node)

Scroll to the given node.

Parameters:

Name Type Description Default
node TreeNode[TreeDataType]

Node to scroll in to view.

required

select_node(node)

Move the cursor to the given node, or reset cursor.

Parameters:

Name Type Description Default
node TreeNode[TreeDataType] | None

A tree node, or None to reset cursor.

required

validate_cursor_line(value)

Prevent cursor line from going outside of range.

validate_guide_depth(value)

Restrict guide depth to reasonable range.