# wx.TreeCtrl¶

A tree control presents information as a hierarchy, with items that may be expanded to show further items.

Items in a tree control are referenced by wx.TreeItemId handles, which may be tested for validity by calling wx.TreeItemId.IsOk .

A similar control with a fully native implementation for GTK+ and macOS as well is wx.dataview.DataViewTreeCtrl.

To intercept events from a tree control, use the event table macros described in wx.TreeEvent.

## Window Styles¶

This class supports the following styles:

• wx.TR_EDIT_LABELS: Use this style if you wish the user to be able to edit labels in the tree control.

• wx.TR_NO_BUTTONS: For convenience to document that no buttons are to be drawn.

• wx.TR_HAS_BUTTONS: Use this style to show + and - buttons to the left of parent items.

• wx.TR_TWIST_BUTTONS: Selects alternative style of +/ - buttons and shows rotating (“twisting”) arrows instead. Currently this style is only implemented under Microsoft Windows Vista and later Windows versions and is ignored under the other platforms as enabling it is equivalent to using SystemThemedControl.EnableSystemTheme .

• wx.TR_NO_LINES: Use this style to hide vertical level connectors.

• wx.TR_FULL_ROW_HIGHLIGHT: Use this style to have the background colour and the selection highlight extend over the entire horizontal row of the tree control window. (This flag is ignored under Windows unless you specify TR_NO_LINES as well.)

• wx.TR_LINES_AT_ROOT: Use this style to show lines leading to the root nodes (unless no TR_NO_LINES is also used, in which case no lines are shown). Note that in the MSW version, if this style is omitted, not only the lines, but also the button used for expanding the root item is not shown, which can be unexpected, so it is recommended to always use it.

• wx.TR_HIDE_ROOT: Use this style to suppress the display of the root node, effectively causing the first-level nodes to appear as a series of root nodes.

• wx.TR_ROW_LINES: Use this style to draw a contrasting border between displayed rows.

• wx.TR_HAS_VARIABLE_ROW_HEIGHT: Use this style to cause row heights to be just big enough to fit the content. If not set, all rows use the largest row height. The default is that this flag is unset. Generic only.

• wx.TR_SINGLE: For convenience to document that only one item may be selected at a time. Selecting another item causes the current selection, if any, to be deselected. This is the default.

• wx.TR_MULTIPLE: Use this style to allow a range of items to be selected. If a second range is selected, the current range, if any, is deselected.

• wx.TR_DEFAULT_STYLE: The set of flags that are closest to the defaults for the native control for a particular toolkit.

## Events Emitted by this Class¶

Handlers bound for the following event types will receive a wx.TreeEvent parameter.

• EVT_TREE_BEGIN_DRAG: Begin dragging with the left mouse button. If you want to enable left-dragging you need to intercept this event and explicitly call wx.TreeEvent.Allow , as it’s vetoed by default. Processes a wxEVT_TREE_BEGIN_DRAG event type.

• EVT_TREE_BEGIN_RDRAG: Begin dragging with the right mouse button. If you want to enable right-dragging you need to intercept this event and explicitly call wx.TreeEvent.Allow , as it’s vetoed by default. Processes a wxEVT_TREE_BEGIN_RDRAG event type.

• EVT_TREE_END_DRAG: End dragging with the left or right mouse button. Processes a wxEVT_TREE_END_DRAG event type.

• EVT_TREE_BEGIN_LABEL_EDIT: Begin editing a label. This can be prevented by calling Veto(). Processes a wxEVT_TREE_BEGIN_LABEL_EDIT event type.

• EVT_TREE_END_LABEL_EDIT: Finish editing a label. This can be prevented by calling Veto(). Processes a wxEVT_TREE_END_LABEL_EDIT event type.

• EVT_TREE_DELETE_ITEM: An item was deleted. Processes a wxEVT_TREE_DELETE_ITEM event type.

• EVT_TREE_GET_INFO: Request information from the application. Processes a wxEVT_TREE_GET_INFO event type.

• EVT_TREE_SET_INFO: Information is being supplied. Processes a wxEVT_TREE_SET_INFO event type.

• EVT_TREE_ITEM_ACTIVATED: The item has been activated, i.e. chosen by double clicking it with mouse or from keyboard. Processes a wxEVT_TREE_ITEM_ACTIVATED event type.

• EVT_TREE_ITEM_COLLAPSED: The item has been collapsed. Processes a wxEVT_TREE_ITEM_COLLAPSED event type.

• EVT_TREE_ITEM_COLLAPSING: The item is being collapsed. This can be prevented by calling Veto(). Processes a wxEVT_TREE_ITEM_COLLAPSING event type.

• EVT_TREE_ITEM_EXPANDED: The item has been expanded. Processes a wxEVT_TREE_ITEM_EXPANDED event type.

• EVT_TREE_ITEM_EXPANDING: The item is being expanded. This can be prevented by calling Veto(). Processes a wxEVT_TREE_ITEM_EXPANDING event type.

• EVT_TREE_ITEM_RIGHT_CLICK: The user has clicked the item with the right mouse button. Processes a wxEVT_TREE_ITEM_RIGHT_CLICK event type.

• EVT_TREE_ITEM_MIDDLE_CLICK: The user has clicked the item with the middle mouse button. This is only supported by the generic control. Processes a wxEVT_TREE_ITEM_MIDDLE_CLICK event type.

• EVT_TREE_SEL_CHANGED: Selection has changed. Processes a wxEVT_TREE_SEL_CHANGED event type.

• EVT_TREE_SEL_CHANGING: Selection is changing. This can be prevented by calling Veto(). Processes a wxEVT_TREE_SEL_CHANGING event type.

• EVT_TREE_KEY_DOWN: A key has been pressed. Processes a wxEVT_TREE_KEY_DOWN event type.

• EVT_TREE_ITEM_GETTOOLTIP: The opportunity to set the item tooltip is being given to the application (call wx.TreeEvent.SetToolTip ). Windows only. Processes a wxEVT_TREE_ITEM_GETTOOLTIP event type.

• EVT_TREE_ITEM_MENU: The context menu for the selected item has been requested, either by a right click or by using the menu key. Notice that these events always carry a valid tree item and so are not generated when (right) clicking outside of the items area. If you need to handle such events, consider using wxEVT_CONTEXT_MENU instead. Processes a wxEVT_TREE_ITEM_MENU event type.

• EVT_TREE_STATE_IMAGE_CLICK: The state image has been clicked. Processes a wxEVT_TREE_STATE_IMAGE_CLICK event type.

Win32 notes:

wx.TreeCtrl class uses the standard common treeview control under Win32 implemented in the system library comctl32.dll. Some versions of this library are known to have bugs with handling the tree control colours: the usual symptom is that the expanded items leave black (or otherwise incorrectly coloured) background behind them, especially for the controls using non-default background colour. The recommended solution is to upgrade the comctl32.dll to a newer version: see http://www.microsoft.com/downloads/details.aspx?familyid=cb2cf3a2-8025-4e8f-8511-9b476a8d35d2

## Class Hierarchy¶

Inheritance diagram for class TreeCtrl:

wxMSW

wxMAC

wxGTK

## Methods Summary¶

 __init__ Default Constructor. AddRoot Adds the root node to the tree, returning the new item. AppendItem Appends an item to the end of the branch identified by parent, return a new item id. AssignImageList Sets the normal image list. AssignStateImageList Sets the state image list. ClearFocusedItem Clears the currently focused item. Collapse Collapses the given item. CollapseAll Collapses the root item. CollapseAllChildren Collapses this item and all of its children, recursively. CollapseAndReset Collapses the given item and removes all children. Create Creates the tree control. Delete Deletes the specified item. DeleteAllItems Deletes all items in the control. DeleteChildren Deletes all children of the given item (but not the item itself). EditLabel Starts editing the label of the given item. EnableBellOnNoMatch Enable or disable a beep if there is no match for the currently entered text when searching for the item from keyboard. EnableSystemTheme Can be used to disable the system theme of controls using it by default. EndEditLabel Ends label editing. EnsureVisible Scrolls and/or expands items to ensure that the given item is visible. Expand Expands the given item. ExpandAll Expands all items in the tree. ExpandAllChildren Expands the given item and all its children recursively. GetBoundingRect Returns the rectangle bounding the item. If textOnly is True, GetChildrenCount Returns the number of items in the branch. GetClassDefaultAttributes GetCount Returns the number of items in the control. GetEditControl Returns the edit control being currently used to edit a label. GetFirstChild Returns the first child; call GetNextChild for the next child. GetFirstVisibleItem Returns the first visible item. GetFocusedItem Returns the item last clicked or otherwise selected. GetImageList Returns the normal image list. GetIndent Returns the current tree control indentation. GetItemBackgroundColour Returns the background colour of the item. GetItemData Returns the tree item data associated with the item. GetItemFont Returns the font of the item label. GetItemImage Gets the specified item image. GetItemParent Returns the item’s parent. GetItemState Gets the specified item state. GetItemText Returns the item label. GetItemTextColour Returns the colour of the item label. GetLastChild Returns the last child of the item (or an invalid tree item if this item has no children). GetNextChild Returns the next child; call GetFirstChild for the first child. GetNextSibling Returns the next sibling of the specified item; call GetPrevSibling for the previous sibling. GetNextVisible Returns the next visible item or an invalid item if this item is the last visible one. GetPrevSibling Returns the previous sibling of the specified item; call GetNextSibling for the next sibling. GetPrevVisible Returns the previous visible item or an invalid item if this item is the first visible one. GetQuickBestSize Returns True if the control will use a quick calculation for the best size, looking only at the first and last items. GetRootItem Returns the root item for the tree control. GetSelection Returns the selection, or an invalid item if there is no selection. GetSelections Returns a list of currently selected items in the tree. This function can be called only if the control has the wx.wx.TR_MULTIPLE style. GetSpacing Returns the current tree control spacing. GetStateImageList Returns the state image list (from which application-defined state images are taken). HitTest Calculates which (if any) item is under the given point, returning the tree item id at this point plus extra information flags. InsertItem Inserts an item after a given one (previous). IsBold Returns True if the given item is in bold state. IsEmpty Returns True if the control is empty (i.e. has no items, even no root one). IsExpanded Returns True if the item is expanded (only makes sense if it has children). IsSelected Returns True if the item is selected. IsVisible Returns True if the item is visible on the screen. ItemHasChildren Returns True if the item has children. OnCompareItems Override this function in the derived class to change the sort order of the items in the tree control. PrependItem Appends an item as the first child of parent, return a new item id. ScrollTo Scrolls the specified item into view. SelectChildren Select all the immediate children of the given parent. SelectItem Selects the given item. SetFocusedItem Sets the currently focused item. SetImageList Sets the normal image list. SetIndent Sets the indentation for the tree control. SetItemBackgroundColour Sets the colour of the item’s background. SetItemBold Makes item appear in bold font if bold parameter is True or resets it to the normal state. SetItemData Sets the item client data. SetItemDropHighlight Gives the item the visual feedback for Drag’n’Drop actions, which is useful if something is dragged from the outside onto the tree control (as opposed to a DnD operation within the tree control, which already is implemented internally). SetItemFont Sets the item’s font. SetItemHasChildren Force appearance of the button next to the item. SetItemImage Sets the specified item’s image. SetItemState Sets the specified item state. SetItemText Sets the item label. SetItemTextColour Sets the colour of the item’s text. SetQuickBestSize If True is passed, specifies that the control will use a quick calculation for the best size, looking only at the first and last items. SetSpacing Sets the spacing for the tree control. SetStateImageList Sets the state image list (from which application-defined state images are taken). SetWindowStyle Sets the mode flags associated with the display of the tree control. SortChildren Sorts the children of the given item using OnCompareItems . Toggle Toggles the given item between collapsed and expanded states. ToggleItemSelection Toggles the given item between selected and unselected states. Unselect Removes the selection from the currently selected item (if any). UnselectAll This function either behaves the same as Unselect if the control doesn’t have TR_MULTIPLE style, or removes the selection from all items if it does have this style. UnselectItem Unselects the given item.

## Class API¶

class wx.TreeCtrl(Control)

Possible constructors:

TreeCtrl()

TreeCtrl(parent, id=ID_ANY, pos=DefaultPosition, size=DefaultSize,
style=TR_DEFAULT_STYLE, validator=DefaultValidator,
name=TreeCtrlNameStr)


A tree control presents information as a hierarchy, with items that may be expanded to show further items.

### Methods¶

__init__(self, *args, **kw)

__init__ (self)

Default Constructor.

__init__ (self, parent, id=ID_ANY, pos=DefaultPosition, size=DefaultSize, style=TR_DEFAULT_STYLE, validator=DefaultValidator, name=TreeCtrlNameStr)

Constructor, creating and showing a tree control.

Parameters
• parent (wx.Window) – Parent window. Must not be None.

• id (wx.WindowID) – Window identifier. The value ID_ANY indicates a default value.

• pos (wx.Point) – Window position. If wx.DefaultPosition is specified then a default position is chosen.

• size (wx.Size) – Window size. If wx.DefaultSize is specified then the window is sized appropriately.

• style (long) – Window style. See wx.TreeCtrl.

• validator (wx.Validator) – Window validator.

• name (string) – Window name.

AddRoot(self, text, image=-1, selImage=-1, data=None)

Adds the root node to the tree, returning the new item.

The image and selImage parameters are an index within the normal image list specifying the image to use for unselected and selected items, respectively. If image > -1 and selImage is -1, the same image is used for both selected and unselected items.

Parameters
• text (string) –

• image (int) –

• selImage (int) –

• data (TreeItemData) –

Return type

wx.TreeItemId

AppendItem(self, parent, text, image=-1, selImage=-1, data=None)

Appends an item to the end of the branch identified by parent, return a new item id.

The image and selImage parameters are an index within the normal image list specifying the image to use for unselected and selected items, respectively. If image > -1 and selImage is -1, the same image is used for both selected and unselected items.

Parameters
• parent (wx.TreeItemId) –

• text (string) –

• image (int) –

• selImage (int) –

• data (TreeItemData) –

Return type

wx.TreeItemId

AssignImageList(self, imageList)

Sets the normal image list.

The image list assigned with this method will be automatically deleted by wx.TreeCtrl as appropriate (i.e. it takes ownership of the list).

Parameters

imageList (wx.ImageList) –

AssignStateImageList(self, imageList)

Sets the state image list.

Image list assigned with this method will be automatically deleted by wx.TreeCtrl as appropriate (i.e. it takes ownership of the list).

Parameters

imageList (wx.ImageList) –

ClearFocusedItem(self)

Clears the currently focused item.

New in version 2.9.1.

Collapse(self, item)

Collapses the given item.

Parameters

item (wx.TreeItemId) –

CollapseAll(self)

Collapses the root item.

CollapseAllChildren(self, item)

Collapses this item and all of its children, recursively.

Parameters

item (wx.TreeItemId) –

CollapseAndReset(self, item)

Collapses the given item and removes all children.

Parameters

item (wx.TreeItemId) –

Create(self, parent, id=ID_ANY, pos=DefaultPosition, size=DefaultSize, style=TR_DEFAULT_STYLE, validator=DefaultValidator, name=TreeCtrlNameStr)

Creates the tree control.

See TreeCtrl.__init__ for further details.

Parameters
Return type

bool

Delete(self, item)

Deletes the specified item.

A EVT_TREE_DELETE_ITEM event will be generated.

This function may cause a subsequent call to GetNextChild to fail.

Parameters

item (wx.TreeItemId) –

DeleteAllItems(self)

Deletes all items in the control.

This function generates wxEVT_TREE_DELETE_ITEM events for each item being deleted, including the root one if it is shown, i.e. unless wx.TR_HIDE_ROOT style is used.

DeleteChildren(self, item)

Deletes all children of the given item (but not the item itself).

A wxEVT_TREE_DELETE_ITEM event will be generated for every item being deleted.

If you have called SetItemHasChildren , you may need to call it again since DeleteChildren does not automatically clear the setting.

Parameters

item (wx.TreeItemId) –

EditLabel(self, item)

Starts editing the label of the given item.

This function generates a EVT_TREE_BEGIN_LABEL_EDIT event which can be vetoed so that no text control will appear for in-place editing.

If the user changed the label (i.e. s/he does not press ESC or leave the text control without changes, a EVT_TREE_END_LABEL_EDIT event will be sent which can be vetoed as well.

Parameters

item (wx.TreeItemId) –

Return type

wx.TextCtrl

EnableBellOnNoMatch(self, on=True)

Enable or disable a beep if there is no match for the currently entered text when searching for the item from keyboard.

The default is to not beep in this case except in wxMSW where the beep is always generated by the native control and cannot be disabled, i.e. calls to this function do nothing there.

Parameters

on (bool) –

New in version 2.9.5.

EnableSystemTheme(self, enable=True)

Can be used to disable the system theme of controls using it by default.

On Windows there an alternative theme available for the list and list-like controls since Windows Vista. This theme is used by Windows Explorer list and tree view and so is arguably more familiar to the users than the standard appearance of these controls. This class automatically uses the new theme, but if that is not desired then this method can be used to turn it off.

Please note that this method should be called before the widget is actually created, using the 2-phase create pattern. Something like this:

# This creates the object, but not the window
widget = wx.TreeCtrl()

# Disable the system theme
widget.EnableSystemTheme(False)

# Now create the window
widget.Create(parent, wx.wx.ID_ANY)


This method has no effect on other platorms

Parameters

enable (bool) –

EndEditLabel(self, item, discardChanges=False)

Ends label editing.

If cancelEdit is True, the edit will be cancelled.

Parameters

Note

This function is currently supported under Windows only.

EnsureVisible(self, item)

Scrolls and/or expands items to ensure that the given item is visible.

This method can be used, and will work, even while the window is frozen (see wx.Window.Freeze ).

Parameters

item (wx.TreeItemId) –

Expand(self, item)

Expands the given item.

Parameters

item (wx.TreeItemId) –

ExpandAll(self)

Expands all items in the tree.

ExpandAllChildren(self, item)

Expands the given item and all its children recursively.

Parameters

item (wx.TreeItemId) –

GetBoundingRect(self, item, textOnly=False)

Returns the rectangle bounding the item. If textOnly is True, only the rectangle around the item’s label will be returned, otherwise the item’s image is also taken into account. The return value may be None if the rectangle was not successfully retrieved, such as if the item is currently not visible.

Return type

PyObject

GetChildrenCount(self, item, recursively=True)

Returns the number of items in the branch.

If recursively is True, returns the total number of descendants, otherwise only one level of children is counted.

Parameters
Return type

int

static GetClassDefaultAttributes(variant=WINDOW_VARIANT_NORMAL)
Parameters

variant (WindowVariant) –

Return type

wx.VisualAttributes

GetCount(self)

Returns the number of items in the control.

Return type

int

GetEditControl(self)

Returns the edit control being currently used to edit a label.

Returns None if no label is being edited.

Return type

wx.TextCtrl

Note

This is currently only implemented for wxMSW.

GetFirstChild(self, item)

Returns the first child; call GetNextChild for the next child.

For this enumeration function you must pass in a ‘cookie’ parameter which is opaque for the application but is necessary for the library to make these functions reentrant (i.e. allow more than one enumeration on one and the same object simultaneously). The cookie passed to GetFirstChild and GetNextChild should be the same variable.

Returns an invalid tree item (i.e. wx.TreeItemId.IsOk returns False) if there are no further children.

GetFirstVisibleItem(self)

Returns the first visible item.

Return type

wx.TreeItemId

GetFocusedItem(self)

Returns the item last clicked or otherwise selected.

Unlike GetSelection , it can be used whether or not the control has the TR_MULTIPLE style.

Return type

wx.TreeItemId

New in version 2.9.1.

GetImageList(self)

Returns the normal image list.

Return type

wx.ImageList

GetIndent(self)

Returns the current tree control indentation.

Return type

int

GetItemBackgroundColour(self, item)

Returns the background colour of the item.

Parameters

item (wx.TreeItemId) –

Return type

wx.Colour

GetItemData(self, item)

Returns the tree item data associated with the item.

GetItemFont(self, item)

Returns the font of the item label.

If the font hadn’t been explicitly set for the specified item with SetItemFont , returns an invalid wx.NullFont font. GetFont can be used to retrieve the global tree control font used for the items without any specific font.

Parameters

item (wx.TreeItemId) –

Return type

wx.Font

GetItemImage(self, item, which=TreeItemIcon_Normal)

Gets the specified item image.

The value of which may be:

• wx.TreeItemIcon_Normal : to get the normal item image.

• wx.TreeItemIcon_Selected : to get the selected item image (i.e. the image which is shown when the item is currently selected).

• wx.TreeItemIcon_Expanded : to get the expanded image (this only makes sense for items which have children - then this image is shown when the item is expanded and the normal image is shown when it is collapsed).

• wx.TreeItemIcon_SelectedExpanded : to get the selected expanded image (which is shown when an expanded item is currently selected).

Parameters
Return type

int

GetItemParent(self, item)

Returns the item’s parent.

Parameters

item (wx.TreeItemId) –

Return type

wx.TreeItemId

GetItemState(self, item)

Gets the specified item state.

Parameters

item (wx.TreeItemId) –

Return type

int

GetItemText(self, item)

Returns the item label.

Parameters

item (wx.TreeItemId) –

Return type

string

GetItemTextColour(self, item)

Returns the colour of the item label.

Parameters

item (wx.TreeItemId) –

Return type

wx.Colour

GetLastChild(self, item)

Returns the last child of the item (or an invalid tree item if this item has no children).

Parameters

item (wx.TreeItemId) –

Return type

wx.TreeItemId

GetNextChild(self, item, cookie)

Returns the next child; call GetFirstChild for the first child.

For this enumeration function you must pass in a ‘cookie’ parameter which is opaque for the application but is necessary for the library to make these functions reentrant (i.e. allow more than one enumeration on one and the same object simultaneously). The cookie passed to GetFirstChild and GetNextChild should be the same.

Returns an invalid tree item if there are no further children.

GetNextSibling(self, item)

Returns the next sibling of the specified item; call GetPrevSibling for the previous sibling.

Returns an invalid tree item if there are no further siblings.

Parameters

item (wx.TreeItemId) –

Return type

wx.TreeItemId

GetNextVisible(self, item)

Returns the next visible item or an invalid item if this item is the last visible one.

Parameters

item (wx.TreeItemId) –

Return type

wx.TreeItemId

Note

The item itself must be visible.

GetPrevSibling(self, item)

Returns the previous sibling of the specified item; call GetNextSibling for the next sibling.

Returns an invalid tree item if there are no further children.

Parameters

item (wx.TreeItemId) –

Return type

wx.TreeItemId

GetPrevVisible(self, item)

Returns the previous visible item or an invalid item if this item is the first visible one.

Parameters

item (wx.TreeItemId) –

Return type

wx.TreeItemId

Note

The item itself must be visible.

GetQuickBestSize(self)

Returns True if the control will use a quick calculation for the best size, looking only at the first and last items.

The default is False.

Return type

bool

GetRootItem(self)

Returns the root item for the tree control.

Return type

wx.TreeItemId

GetSelection(self)

Returns the selection, or an invalid item if there is no selection.

This function only works with the controls without TR_MULTIPLE style, use GetSelections for the controls which do have this style or, if a single item is wanted, use GetFocusedItem .

Return type

wx.TreeItemId

GetSelections(self)

Returns a list of currently selected items in the tree. This function can be called only if the control has the wx.wx.TR_MULTIPLE style.

Return type

PyObject

GetSpacing(self)

Returns the current tree control spacing.

This is the number of horizontal pixels between the buttons and the state images.

Return type

int

GetStateImageList(self)

Returns the state image list (from which application-defined state images are taken).

Return type

wx.ImageList

HitTest(self, point, flags)

Calculates which (if any) item is under the given point, returning the tree item id at this point plus extra information flags.

flags is a bitlist of the following:

• TREE_HITTEST_ABOVE: Above the client area.

• TREE_HITTEST_BELOW: Below the client area.

• TREE_HITTEST_NOWHERE: In the client area but below the last item.

• TREE_HITTEST_ONITEMBUTTON: On the button associated with an item.

• TREE_HITTEST_ONITEMICON: On the bitmap associated with an item.

• TREE_HITTEST_ONITEMINDENT: In the indentation associated with an item.

• TREE_HITTEST_ONITEMLABEL: On the label (string) associated with an item.

• TREE_HITTEST_ONITEMRIGHT: In the area to the right of an item.

• TREE_HITTEST_ONITEMSTATEICON: On the state icon for a tree view item that is in a user-defined state.

• TREE_HITTEST_TOLEFT: To the right of the client area.

• TREE_HITTEST_TORIGHT: To the left of the client area.

InsertItem(self, *args, **kw)

InsertItem (self, parent, previous, text, image=-1, selImage=-1, data=None)

Inserts an item after a given one (previous).

The image and selImage parameters are an index within the normal image list specifying the image to use for unselected and selected items, respectively. If image > -1 and selImage is -1, the same image is used for both selected and unselected items.

Parameters
• parent (wx.TreeItemId) –

• previous (wx.TreeItemId) –

• text (string) –

• image (int) –

• selImage (int) –

• data (TreeItemData) –

Return type

wx.TreeItemId

InsertItem (self, parent, pos, text, image=-1, selImage=-1, data=None)

Inserts an item before one identified by its position (pos).

pos must be less than or equal to the number of children.

The image and selImage parameters are an index within the normal image list specifying the image to use for unselected and selected items, respectively. If image > -1 and selImage is -1, the same image is used for both selected and unselected items.

Parameters
• parent (wx.TreeItemId) –

• pos (int) –

• text (string) –

• image (int) –

• selImage (int) –

• data (TreeItemData) –

Return type

wx.TreeItemId

IsBold(self, item)

Returns True if the given item is in bold state.

Parameters

item (wx.TreeItemId) –

Return type

bool

IsEmpty(self)

Returns True if the control is empty (i.e. has no items, even no root one).

Return type

bool

IsExpanded(self, item)

Returns True if the item is expanded (only makes sense if it has children).

Parameters

item (wx.TreeItemId) –

Return type

bool

IsSelected(self, item)

Returns True if the item is selected.

Parameters

item (wx.TreeItemId) –

Return type

bool

IsVisible(self, item)

Returns True if the item is visible on the screen.

Parameters

item (wx.TreeItemId) –

Return type

bool

ItemHasChildren(self, item)

Returns True if the item has children.

Parameters

item (wx.TreeItemId) –

Return type

bool

OnCompareItems(self, item1, item2)

Override this function in the derived class to change the sort order of the items in the tree control.

The function should return a negative, zero or positive value if the first item is less than, equal to or greater than the second one.

Please note that you must use RTTI macros DECLARE_DYNAMIC_CLASS and IMPLEMENT_DYNAMIC_CLASS if you override this function because otherwise the base class considers that it is not overridden and uses the default comparison, i.e. sorts the items alphabetically, which allows it optimize away the calls to the virtual function completely.

Parameters
Return type

int

PrependItem(self, parent, text, image=-1, selImage=-1, data=None)

Appends an item as the first child of parent, return a new item id.

The image and selImage parameters are an index within the normal image list specifying the image to use for unselected and selected items, respectively. If image > -1 and selImage is -1, the same image is used for both selected and unselected items.

Parameters
• parent (wx.TreeItemId) –

• text (string) –

• image (int) –

• selImage (int) –

• data (TreeItemData) –

Return type

wx.TreeItemId

ScrollTo(self, item)

Scrolls the specified item into view.

Note that this method doesn’t work while the window is frozen (See wx.Window.Freeze ), at least under MSW.

Parameters

item (wx.TreeItemId) –

SelectChildren(self, parent)

Select all the immediate children of the given parent.

This function can be used with multiselection controls only.

Parameters

parent (wx.TreeItemId) –

New in version 2.9.1.

SelectItem(self, item, select=True)

Selects the given item.

In multiple selection controls, can be also used to deselect a currently selected item if the value of select is False.

Notice that calling this method will generate wxEVT_TREE_SEL_CHANGING and wxEVT_TREE_SEL_CHANGED events and that the change could be vetoed by the former event handler.

Parameters

SetFocusedItem(self, item)

Sets the currently focused item.

Parameters

item (wx.TreeItemId) – The item to make the current one. It must be valid.

New in version 2.9.1.

SetImageList(self, imageList)

Sets the normal image list.

The image list assigned with this method will not be deleted by wx.TreeCtrl’s destructor, you must delete it yourself.

Parameters

imageList (wx.ImageList) –

SetIndent(self, indent)

Sets the indentation for the tree control.

Parameters

indent (int) –

SetItemBackgroundColour(self, item, col)

Sets the colour of the item’s background.

Parameters

SetItemBold(self, item, bold=True)

Makes item appear in bold font if bold parameter is True or resets it to the normal state.

Parameters

SetItemData(self, item, data)

Sets the item client data.

Notice that the client data previously associated with the item (if any) is not freed by this function and so calling this function multiple times for the same item will result in memory leaks unless you delete the old item data pointer yourself.

SetItemDropHighlight(self, item, highlight=True)

Gives the item the visual feedback for Drag’n’Drop actions, which is useful if something is dragged from the outside onto the tree control (as opposed to a DnD operation within the tree control, which already is implemented internally).

Parameters

SetItemFont(self, item, font)

Sets the item’s font.

All items in the tree should have the same height to avoid text clipping, so the fonts height should be the same for all of them, although font attributes may vary.

Parameters

SetItemHasChildren(self, item, hasChildren=True)

Force appearance of the button next to the item.

This is useful to allow the user to expand the items which don’t have any children now, but instead adding them only when needed, thus minimizing memory usage and loading time.

Parameters

SetItemImage(self, item, image, which=TreeItemIcon_Normal)

Sets the specified item’s image.

See GetItemImage for the description of the which parameter.

Parameters

SetItemState(self, item, state)

Sets the specified item state.

The value of state may be an index into the state image list, or one of the special values:

• TREE_ITEMSTATE_NONE: to disable the item state (the state image will be not displayed).

• TREE_ITEMSTATE_NEXT: to set the next item state.

• TREE_ITEMSTATE_PREV: to set the previous item state.

Parameters

SetItemText(self, item, text)

Sets the item label.

Parameters

SetItemTextColour(self, item, col)

Sets the colour of the item’s text.

Parameters

SetQuickBestSize(self, quickBestSize)

If True is passed, specifies that the control will use a quick calculation for the best size, looking only at the first and last items.

Otherwise, it will look at all items. The default is False.

Parameters

quickBestSize (bool) –

SetSpacing(self, spacing)

Sets the spacing for the tree control.

Spacing is the number of horizontal pixels between the buttons and the state images. This has no effect under wxMSW.

Parameters

spacing (int) –

SetStateImageList(self, imageList)

Sets the state image list (from which application-defined state images are taken).

Image list assigned with this method will not be deleted by wx.TreeCtrl’s destructor, you must delete it yourself.

Parameters

imageList (wx.ImageList) –

SetWindowStyle(self, styles)

Sets the mode flags associated with the display of the tree control.

The new mode takes effect immediately.

Parameters

styles (long) –

Note

Generic only; MSW ignores changes.

SortChildren(self, item)

Sorts the children of the given item using OnCompareItems .

You should override that method to change the sort order (the default is ascending case-sensitive alphabetical order).

Parameters

item (wx.TreeItemId) –

TreeItemData , OnCompareItems

Toggle(self, item)

Toggles the given item between collapsed and expanded states.

Parameters

item (wx.TreeItemId) –

ToggleItemSelection(self, item)

Toggles the given item between selected and unselected states.

For multiselection controls only.

Parameters

item (wx.TreeItemId) –

Unselect(self)

Removes the selection from the currently selected item (if any).

UnselectAll(self)

This function either behaves the same as Unselect if the control doesn’t have TR_MULTIPLE style, or removes the selection from all items if it does have this style.

UnselectItem(self, item)

Unselects the given item.

This works in multiselection controls only.

Parameters

item (wx.TreeItemId) –

### Properties¶

BoundingRect

Count

EditControl

FirstVisibleItem

FocusedItem

ImageList

Indent

QuickBestSize

RootItem

Selection

Selections

Spacing

StateImageList