Tree

macOS

GTK+

Windows

iOS

Android

Django

y

y

y

The tree widget is still under development.

Usage

import toga

tree = toga.Tree(['Navigate'])

tree.insert(None, None, 'root1')

root2 = tree.insert(None, None, 'root2')

tree.insert(root2, None, 'root2.1')
root2_2 = tree.insert(root2, None, 'root2.2')

tree.insert(root2_2, None, 'root2.2.1')
tree.insert(root2_2, None, 'root2.2.2')
tree.insert(root2_2, None, 'root2.2.3')

Reference

class toga.widgets.tree.Tree(headings, id=None, style=None, data=None, accessors=None, multiple_select=False, on_select=None, on_double_click=None, factory=None)

Tree Widget.

Parameters
  • headings – The list of headings for the interface.

  • id – An identifier for this widget.

  • style – An optional style object. If no style is provided then a new one will be created for the widget.

  • data

    The data to display in the widget. Can be an instance of TreeSource, a list, dict or tuple with data to display in the tree widget, or a class instance which implements the interface of TreeSource. Entries can be:

    • any Python object value with a string representation. This string will be shown in the widget. If value has an attribute icon, instance of (Icon), the icon will be shown in front of the text.

    • a tuple (icon, value) where again the string representation of value will be used as text.

  • accessors – Optional; a list of attributes to access the value in the columns. If not given, the headings will be taken.

  • multiple_select – Boolean; if True, allows for the selection of multiple rows. Defaults to False.

  • on_select – A handler to be invoked when the user selects one or multiple rows.

  • on_double_click – A handler to be invoked when the user double clicks a row.

MIN_HEIGHT = 100
MIN_WIDTH = 100
add(*children)

Add nodes as children of this one. If a node already has a different parent, it will be moved over. This does nothing if a node already is a child of this node.

Parameters

children – Nodes to add as children of this node.

Raises

ValueError – If this node is a leaf, and cannot have children.

property app

The App to which this widget belongs. On setting the app we also iterate over all children of this widget and set them to the same app.

Returns

The App to which this widget belongs.

Raises

ValueError – If the widget is already associated with another app.

property can_have_children

Determine if the node can have children.

This does not resolve whether there actually are any children; it only confirms whether children are theoretically allowed.

property children

The children of this node. This always returns a list, even if the node is a leaf and cannot have children.

Returns

A list of the children for this widget.

property data

The data source of the tree :rtype: dict

Type

returns

property enabled
focus()
property id

The node identifier. This id can be used to target styling directives.

Returns

The widgets identifier as a str.

insert(index, child)

Insert a node as a child of this one. If the node already has a different parent, it will be moved over. This does nothing if the node already is a child of this node.

Parameters
  • index – Position of child node.

  • child – A node to insert as a child of this node.

Raises

ValueError – If this node is a leaf, and cannot have children.

property multiple_select

Does the table allow multiple rows to be selected?

property on_double_click

The callable function for when a node on the Tree is selected. The provided callback function has to accept two arguments tree (Tree) and node (Node or None).

Return type

callable

property on_select

The callable function for when a node on the Tree is selected. The provided callback function has to accept two arguments tree (Tree) and node (Node or None).

Return type

callable

property parent

The parent of this node.

Returns

The parent of this node. Returns None if this node is the root node.

refresh()

Refresh the layout and appearance of the tree this node is contained in.

refresh_sublayouts()
remove(*children)

Remove child nodes of this node. This does nothing if a given node is not a child of this node.

Parameters

children – Child nodes to remove.

Raises

ValueError – If this node is a leaf, and cannot have children.

property root

The root of the tree containing this node.

Returns

The root node. Returns self if this node is the root node.

property selection

The current selection of the table.

A value of None indicates no selection. If the tree allows multiple selection, returns a list of selected data nodes. Otherwise, returns a single data node.

property tab_index
property window

The Window to which this widget belongs. On setting the window, we automatically update all children of this widget to belong to the same window.

Returns

The Window to which the widget belongs.