TTkTree🌶️

class TTkTree(*, treeWidget: TTkTreeWidget = None, **kwargs)[source]🌶️

Bases: TTkAbstractScrollArea

TTkTree is a container widget which place TTkTreeWidget in a scrolling area with on-demand scroll bars.

The TTkTreeWidget class is a convenience class that provides a standard tree widget with a classic item-based interface.

This class is based on TTk’s Model/View architecture and uses a default model to hold items, each of which is a TTkTreeWidgetItem.

In its simplest form, a tree widget can be constructed in the following way:

import TermTk as ttk

root = ttk.TTk()

tree = ttk.TTkTree(parent=root,size=(80,24))
tree.setHeaderLabels(["Column 1", "Column 2", "Column 3"])

top = ttk.TTkTreeWidgetItem(["String A", "String B", "String C"])

tree.addTopLevelItem(top)

for i in range(5):
    child = ttk.TTkTreeWidgetItem(["Child A" + str(i), "Child B" + str(i), "Child C" + str(i)])
    top.addChild(child)

root.mainloop()

Before items can be added to the tree widget, the number of columns must be set with setHeaderLabels(). This allows each item to have one label.

The tree can have a header that contains a section for each column in the widget. It is easiest to set up the labels for each section by supplying a list of strings with setHeaderLabels().

The items in the tree can be sorted by column according to a predefined sort order. If sorting is enabled, the user can sort the items by clicking on a column header. Sorting can be enabled or disabled by calling setSortingEnabled(). The isSortingEnabled() function indicates whether sorting is enabled.

Parameters:

treeWidget (TTkTreeWidget, optional) – a custom Tree Widget to be used instead of the default one.

TTkTreeWidget’s forwarded init params:

Parameters:

TTkContainer’s inherited init params:

Parameters:
  • layout (TermTk.TTkLayouts) – the layout of this widget, optional, defaults to TTkLayout

  • padding (TTkPadding) – the padding (top, bottom, left, right) of the widget, defaults to (0,0,0,0)

  • paddingTop (int) – the Top padding, override Top padding if already defined, optional, default=0 if padding is not defined

  • paddingBottom (int) – the Bottom padding, override Bottom padding if already defined, optional, default=0 if padding is not defined

  • paddingLeft (int) – the Left padding, override Left padding if already defined, optional, default=0 if padding is not defined

  • paddingRight (int) – the Right padding, override Right padding if already defined, optional, default=0 if padding is not defined

  • forwardStyle (bool) – [Experimental] any change of style will reflect the children, defaults to False

TTkWidget’s inherited init params:

Parameters:
  • name (str, optional) – the name of the widget, defaults to “”

  • parent (TTkWidget, optional) – the parent widget, defaults to None

  • x (int, optional) – the x position, defaults to 0

  • y (int, optional) – the y position, defaults to 0

  • pos ((int,int), optional) – the [x,y] position (override the previously defined x, y), defaults to (x,y)

  • width (int, optional) – the width of the widget, defaults to 0

  • height (int, optional) – the height of the widget, defaults to 0

  • size ((int,int), optional) – the size [width, height] of the widget (override the previously defined sizes), defaults to (width,height)

  • maxWidth (int, optional) – the maxWidth of the widget, defaults to 0x10000

  • maxHeight (int, optional) – the maxHeight of the widget, defaults to 0x10000

  • maxSize ((int,int), optional) – the max [width,height] of the widget, optional, defaults to (maxWidth,maxHeight)

  • minWidth (int, optional) – the minWidth of the widget, defaults to 0

  • minHeight (int, optional) – the minHeight of the widget, defaults to 0

  • minSize ((int,int), optional) – the minSize [width,height] of the widget, optional, defaults to (minWidth,minHeight)

  • toolTip (TTkString, optional) – This property holds the widget’s tooltip, defaults to ‘’

  • style (dict, optional) – this field hold the custom style to be used by this widget

  • addStyle (dict, optional) – this field is required to override/merge the new style on top of the current one, useful if only few params need to be changed

  • visible (bool, optional) – the visibility, optional, defaults to True

  • enabled (bool, optional) – the ability to handle input events, optional, defaults to True

TMouseEvents’s inherited init params:

Initialize self. See help(type(self)) for accurate signature.

TDragEvents’s inherited init params:

Initialize self. See help(type(self)) for accurate signature.

Signals🌶️

Signals linked to: TTkTreeWidget

TTkTreeWidget.itemActivated

This signal is emitted when the user activates an item by double-clicking or pressing a special key (e.g., Enter).

TTkTreeWidget.itemChanged

This signal is emitted when the contents of the column in the specified item changes.

TTkTreeWidget.itemClicked

This signal is emitted when the user clicks inside the widget.

TTkTreeWidget.itemCollapsed

This signal is emitted when the specified item is collapsed so that none of its children are displayed.

TTkTreeWidget.itemDoubleClicked

This signal is emitted when the user double clicks inside the widget.

TTkTreeWidget.itemExpanded

This signal is emitted when the specified item is expanded so that all of its children are displayed.

TTkTree signals:

closed

This signal is emitted whenever the widget is closed

currentStyleChanged

This signal is emitted whenever the widget stye change

focusChanged

This signal is emitted whenever the focus status change i.e. with the setFocus() or clearFocus() methods.

sizeChanged

This signal is emitted whenever the widget size change

Slots🌶️

Slots linked to: TTkTreeWidget

TTkTreeWidget.collapseAll()

Collapse all collapsable items.

TTkTreeWidget.expandAll()

Expands all expandable items.

TTkTree slots:

Slots Inherited from: TTkContainer

hide()

hide the widget

show()

show the widget

Slots Inherited from: TTkWidget

close()

Close (Destroy/Remove) the widget

hide()

hide the widget

lowerWidget()

Lower the Widget below its relatives

raiseWidget([raiseParent])

Raise the Widget above its relatives

setDisabled([disabled])

This property holds whether the widget is disabled

setEnabled([enabled])

This property holds whether the widget is enabled

setFocus()

Focus the widget

setVisible(visible)

Set the visibility status of this widget

show()

show the widget

update([repaint, updateLayout, updateParent])

Update the scroll area and viewport

Members🌶️

closed: pyTTkSignal🌶️

This signal is emitted whenever the widget is closed

Parameters:

widget (TTkWidget) – the widget closed (=self)

currentStyleChanged: pyTTkSignal🌶️

This signal is emitted whenever the widget stye change

Parameters:

style (dict) – the new style applied

focusChanged: pyTTkSignal🌶️

This signal is emitted whenever the focus status change i.e. with the setFocus() or clearFocus() methods

Parameters:

status (bool) – the curent focus status

sizeChanged: pyTTkSignal🌶️

This signal is emitted whenever the widget size change

Parameters:
  • width (int) – the new widget width

  • height (int) – the new widget height

Methods🌶️

Methods linked to: TTkTreeWidget

TTkTreeWidget.addTopLevelItem(item)

Appends the item as a top-level item in the widget.

TTkTreeWidget.addTopLevelItems(items)

Appends the list of items as a top-level items in the widget.

TTkTreeWidget.clear()

Clears the tree widget by removing all of its items and selections.

TTkTreeWidget.clearSelection()

Deselects all selected items.

TTkTreeWidget.collapseAll()

Collapse all collapsable items.

TTkTreeWidget.deselectItem(item)

Removes the specified item from the current selection.

TTkTreeWidget.dragDropMode()

TTkTreeWidget.expandAll()

Expands all expandable items.

TTkTreeWidget.indexOfTopLevelItem(item)

Returns the index of the given top-level item, or -1 if the item cannot be found.

TTkTreeWidget.invisibleRootItem()

Returns the tree widget's invisible root item.

TTkTreeWidget.itemAt(pos)

Return the item at the vertical position

TTkTreeWidget.resizeColumnToContents(column)

rwsize the width of the column requestedto its content

TTkTreeWidget.selectItem(item)

Adds the specified item to the current selection.

TTkTreeWidget.selectedItems()

Returns a list of all selected non-hidden items.

TTkTreeWidget.setColumnWidth(column, width)

Set the width of the column requested

TTkTreeWidget.setCurrentItem(item)

Selects the specified item as the current one.

TTkTreeWidget.setDragDropMode(dndMode)

TTkTreeWidget.setHeaderLabels(labels)

Adds a column in the header for each item in the labels list, and sets the label for each column.

TTkTreeWidget.setSelectionMode(mode)

Sets the current selection model to the given selectionModel.

TTkTreeWidget.sortColumn()

Returns the column used to sort the contents of the widget.

TTkTreeWidget.sortItems(col, order)

Sorts the items in the widget in the specified order by the values in the given column.

TTkTreeWidget.takeTopLevelItem(index)

Removes the top-level item at the given index in the tree and returns it, otherwise returns None;

TTkTreeWidget.topLevelItem(index)

Returns the top level item at the given index, or None if the item does not exist.

TTkTree methods:

addTopLevelItem(item: TTkTreeWidgetItem) None[source]🌶️

See also

this method is forwarded to TTkTreeWidget.addTopLevelItem()

Appends the item as a top-level item in the widget.

Parameters:

item (TTkTreeWidgetItem) – the item to be added.

addTopLevelItems(items: List[TTkTreeWidgetItem]) None[source]🌶️

See also

this method is forwarded to TTkTreeWidget.addTopLevelItems()

Appends the list of items as a top-level items in the widget.

Parameters:

item (List[TTkTreeWidgetItem]) – the item to be added.

clear() None[source]🌶️

See also

this method is forwarded to TTkTreeWidget.clear()

Clears the tree widget by removing all of its items and selections.

clearSelection() None[source]🌶️

See also

this method is forwarded to TTkTreeWidget.clearSelection()

Deselects all selected items.

collapseAll() None[source]🌶️

See also

this method is forwarded to TTkTreeWidget.collapseAll()

Collapse all collapsable items.

deselectItem(item: TTkTreeWidgetItem) None[source]🌶️

See also

this method is forwarded to TTkTreeWidget.deselectItem()

Removes the specified item from the current selection.

Parameters:

item (TTkTreeWidgetItem) – the item to be deselected

dragDropMode() DragDropMode[source]🌶️

See also

this method is forwarded to TTkTreeWidget.dragDropMode()

dragDropMode

expandAll() None[source]🌶️

See also

this method is forwarded to TTkTreeWidget.expandAll()

Expands all expandable items.

indexOfTopLevelItem(item: TTkTreeWidgetItem) int[source]🌶️

See also

this method is forwarded to TTkTreeWidget.indexOfTopLevelItem()

Returns the index of the given top-level item, or -1 if the item cannot be found.

Return type:

int

invisibleRootItem() TTkTreeWidgetItem[source]🌶️

See also

this method is forwarded to TTkTreeWidget.invisibleRootItem()

Returns the tree widget’s invisible root item.

The invisible root item provides access to the tree widget’s top-level items through the TTkTreeWidgetItem API, making it possible to write functions that can treat top-level items and their children in a uniform way; for example, recursive functions.

Returns:

the root Item

Return type:

TTkTreeWidgetItem

itemAt(pos: int) TTkTreeWidgetItem | None[source]🌶️

See also

this method is forwarded to TTkTreeWidget.itemAt()

Return the item at the vertical position

Parameters:

pos (int) – y coordinate

Returns:

The item at the (pos) position if available

Return type:

TTkTreeWidgetItem or None if no item is available

resizeColumnToContents(column: int) None[source]🌶️

See also

this method is forwarded to TTkTreeWidget.resizeColumnToContents()

rwsize the width of the column requestedto its content

Parameters:

column (int) – the column position

selectItem(item: TTkTreeWidgetItem) None[source]🌶️

See also

this method is forwarded to TTkTreeWidget.selectItem()

Adds the specified item to the current selection.

In single selection mode this replaces the previous selection.

Parameters:

item (TTkTreeWidgetItem) – the item to be selected

selectedItems() List[TTkTreeWidgetItem][source]🌶️

See also

this method is forwarded to TTkTreeWidget.selectedItems()

Returns a list of all selected non-hidden items.

Return type:

List[TTkTreeWidgetItem]

setColumnWidth(column: int, width: int) None[source]🌶️

See also

this method is forwarded to TTkTreeWidget.setColumnWidth()

Set the width of the column requested

Parameters:

column (int) – the column position

Return type:

int

setCurrentItem(item: TTkTreeWidgetItem | None) None[source]🌶️

See also

this method is forwarded to TTkTreeWidget.setCurrentItem()

Selects the specified item as the current one.

Parameters:

item (TTkTreeWidgetItem or None) – the item to be selected, None clears the selection

setDragDropMode(dndMode: DragDropMode)[source]🌶️

See also

this method is forwarded to TTkTreeWidget.setDragDropMode()

setDragDropMode

setHeaderLabels(labels: List[TTkString]) None[source]🌶️

See also

this method is forwarded to TTkTreeWidget.setHeaderLabels()

Adds a column in the header for each item in the labels list, and sets the label for each column.

Parameters:

labels (List[TTkString]) – the list of labels

setSelectionMode(mode: SelectionMode) None[source]🌶️

See also

this method is forwarded to TTkTreeWidget.setSelectionMode()

Sets the current selection model to the given selectionModel.

Parameters:

mode (TTkK.SelectionMode) – the selection mode used in this tree

sortColumn() int[source]🌶️

See also

this method is forwarded to TTkTreeWidget.sortColumn()

Returns the column used to sort the contents of the widget. -1 in case no column sort is used

Return type:

int

sortItems(col: int, order: SortOrder) None[source]🌶️

See also

this method is forwarded to TTkTreeWidget.sortItems()

Sorts the items in the widget in the specified order by the values in the given column.

Parameters:
  • col (int) – the column used as reference for the sorting

  • order (TTkK.SortOrder) – the sorting order

takeTopLevelItem(index: int) TTkTreeWidgetItem | None[source]🌶️

See also

this method is forwarded to TTkTreeWidget.takeTopLevelItem()

Removes the top-level item at the given index in the tree and returns it, otherwise returns None;

Parameters:

index (int) – the index of the item

Return type:

Optional[TTkTreeWidgetItem]

topLevelItem(index) TTkTreeWidgetItem | None[source]🌶️

See also

this method is forwarded to TTkTreeWidget.topLevelItem()

Returns the top level item at the given index, or None if the item does not exist.

Parameters:

index (int) – the index of the item

Return type:

Optional[TTkTreeWidgetItem]

Methods Inherited from: TTkAbstractScrollArea

resizeEvent(w, h)

Handle resize events

setHorizontalScrollBarPolicy(policy)

Set the horizontal scroll bar policy

setVerticalScrollBarPolicy(policy)

Set the vertical scroll bar policy

setViewport(viewport)

Set the viewport widget

update([repaint, updateLayout, updateParent])

Update the scroll area and viewport

viewport()

Return the current viewport

Methods Inherited from: TTkContainer

addWidget(widget)

getPadding()

Retrieve the TTkContainer's paddings sizes as shown in Layout Topology

getWidgetByName(name)

Return the widget from its name.

hide()

hide the widget

keyEvent(evt)

This event handler, can be reimplemented in a subclass to receive key events for the widget.

layout()

Get the Layout

maximumHeight()

Retrieve the maximum height

maximumWidth()

Retrieve the maximum width

minimumHeight()

Retrieve the minimum height

minimumWidth()

Retrieve the minimum width

paintChildCanvas()

removeWidget(widget)

rootLayout()

This is a root layout mainly used to place items that are not supposed to be inside the main layout (i.e. the menu elements).

setCurrentStyle(*args, **kwargs)

Set the currently active style

setLayout(layout)

Set the Layout used by this widget to place all the child widgets.

setPadding(top, bottom, left, right)

Set the TTkContainer's paddings sizes as shown in Layout Topology

show()

show the widget

update([repaint, updateLayout, updateParent])

Update the scroll area and viewport

Methods Inherited from: TTkWidget

clearFocus()

Remove the Focus state of this widget

close()

Close (Destroy/Remove) the widget

currentStyle()

Retrieve the currently active style

disableWidgetCursor([disable])

Disable or enable the widget cursor

enableWidgetCursor([enable])

Enable or disable the widget cursor

focusInEvent()

Callback triggered when the widget receives focus

focusOutEvent()

Callback triggered when the widget loses focus

focusPolicy()

Retrieve the focus policy of this widget

geometry()

Retrieve the widget geometry

getCanvas()

Retrieve the widget canvas

getPixmap()

Convenience function which return a pixmap representing the current widget status

getWidgetByName(name)

Return the widget from its name.

hasFocus()

This property holds the focus status of this widget

height()

Retrieve the widget height

hide()

hide the widget

isEnabled()

This property holds whether the widget is enabled

isEntered()

Check if the mouse cursor is currently over this widget

isVisible()

Retrieve the visibility status of this widget

isVisibleAndParent()

Check if the widget and all its parents are visible

lowerWidget()

Lower the Widget below its relatives

maxDimension(orientation)

Retrieve the maximum dimension for the given orientation

maximumHeight()

Retrieve the maximum height

maximumSize()

Retrieve the maximum size

maximumWidth()

Retrieve the maximum width

mergeStyle(style)

Merge additional style properties with the existing style

minDimension(orientation)

Retrieve the minimum dimension for the given orientation

minimumHeight()

Retrieve the minimum height

minimumSize()

Retrieve the minimum size

minimumWidth()

Retrieve the minimum width

mouseEvent(evt)

Handle mouse events for this widget and its children

move(x, y)

Move the widget

moveEvent(x, y)

Convenience function, Event Callback triggered after a successful move

name()

Retrieve the name of this widget

paintChildCanvas()

paintEvent(canvas)

Paint Event callback, this need to be overridden in the widget.

parentWidget()

Retrieve the parent widget

pasteEvent(txt)

Callback triggered when a paste event is forwarded to this widget.

pos()

Retrieve the widget position

raiseWidget([raiseParent])

Raise the Widget above its relatives

resize(width, height)

Resize the widget

resizeEvent(w, h)

Handle resize events

setCurrentStyle(*args, **kwargs)

Set the currently active style

setDefaultSize(arg, width, height)

Set default size if not already specified in arguments

setDisabled([disabled])

This property holds whether the widget is disabled

setDropEventProxy(proxy)

setEnabled([enabled])

This property holds whether the widget is enabled

setFocus()

Focus the widget

setFocusPolicy(policy)

This property holds the way the widget accepts keyboard focus

setGeometry(x, y, width, height)

Resize and move the widget

setMaximumHeight(maxh)

Set the maximum height of the widget

setMaximumSize(maxw, maxh)

Set the maximum size of the widget

setMaximumWidth(maxw)

Set the maximum width of the widget

setMinimumHeight(minh)

Set the minimum height of the widget

setMinimumSize(minw, minh)

Set the minimum size of the widget

setMinimumWidth(minw)

Set the minimum width of the widget

setName(name)

Set the name of this Instance

setParent(parent)

Set the parent widget

setStyle([style])

Set the style for the widget

setToolTip(toolTip)

Set the widget tooltip

setVisible(visible)

Set the visibility status of this widget

setWidgetCursor([pos, type])

Set the widget cursor position and type

show()

show the widget

size()

Retrieve the widget size

style()

Retrieve a copy of the widget style dictionary

toolTip()

Retrieve the widget tooltip

update([repaint, updateLayout, updateParent])

Update the scroll area and viewport

widgetItem()

Retrieve the widget item (layout item wrapper)

width()

Retrieve the widget width

x()

Retrieve the horizontal position

y()

Retrieve the vertical position

Methods Inherited from: TMouseEvents

enterEvent(evt)

This event handler, can be reimplemented in a subclass to receive mouse enter events for the widget.

leaveEvent(evt)

This event handler, can be reimplemented in a subclass to receive mouse leave events for the widget.

mouseDoubleClickEvent(evt)

This event handler, can be reimplemented in a subclass to receive mouse click events for the widget.

mouseDragEvent(evt)

This event handler, can be reimplemented in a subclass to receive mouse drag events for the widget.

mouseMoveEvent(evt)

This event handler, can be reimplemented in a subclass to receive mouse move events for the widget.

mousePressEvent(evt)

This event handler, can be reimplemented in a subclass to receive mouse press events for the widget.

mouseReleaseEvent(evt)

This event handler, can be reimplemented in a subclass to receive mouse release events for the widget.

mouseTapEvent(evt)

This event handler, can be reimplemented in a subclass to receive mouse click events for the widget.

wheelEvent(evt)

This event handler, can be reimplemented in a subclass to receive mouse wheel events for the widget.

Methods Inherited from: TKeyEvents

keyEvent(evt)

This event handler, can be reimplemented in a subclass to receive key events for the widget.

Methods Inherited from: TDragEvents

dragEnterEvent(evt)

This event handler, can be reimplemented in a subclass to receive drag events for the widget.

dragLeaveEvent(evt)

This event handler, can be reimplemented in a subclass to receive drag events for the widget.

dragMoveEvent(evt)

This event handler, can be reimplemented in a subclass to receive drag events for the widget.

dropEvent(evt)

This event handler, can be reimplemented in a subclass to receive drag events for the widget.

TTkTree Attributes🌶️

classStyle

itemActivated

itemChanged

itemClicked

itemCollapsed

itemDoubleClicked

itemExpanded