Treeform

class Treeform[source]

Bases: Component

A component for displaying hierarchical tree-like data in a tree widget.

This component provides a flexible way to visualize tree structures with customizable columns and background colors. It supports selection callbacks and can be used in various UI layouts.

Parameters:
treeTree, optional

The tree data structure to display. Defaults to an empty tree.

columnsdict[str, Callable], optional

Dictionary mapping column names to functions that extract values from tree nodes. Defaults to {“Name”: lambda node: node.name, “Value”: lambda node: node.attributes.get(“value”, “”)}.

show_headersbool, optional

Whether to show column headers. Defaults to True.

stretchint, optional

Stretch factor for the tree widget in grid layouts. Defaults to 2.

backgroundsdict[str, Callable], optional

Dictionary mapping column names to functions that return background colors.

actionCallable, optional

Function to call when tree items are selected. Receives the selected node as argument.

Attributes:
widgetQTreeWidget

The Qt tree widget for displaying the data.

treeTree

The tree data structure being displayed.

columnsdict[str, Callable]

Column definitions for the tree display.

stretchint

Stretch factor for layout purposes.

actionCallable or None

Selection action function.

Examples

>>> # Create a simple tree form
>>> treeform = Treeform()
>>> treeform.update()
>>> # Create with custom columns
>>> columns = {"Name": lambda node: node.name, "Type": lambda node: type(node).__name__}
>>> treeform = Treeform(columns=columns)
>>> # Create with selection action
>>> def on_select(node):
...     print(f"Selected: {node.name}")
>>> treeform = Treeform(action=on_select)

Methods

on_item_selection_changed

Handle tree item selection changes.

tree_from_dict

Create a tree structure from a dictionary.

update

Update the tree widget display with the current tree data.

update_from_dict

Update the tree display from a dictionary structure.