wx.TreeCtrl

Inheritance diagram for wx.TreeCtrl:



Description

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.

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

Note

Win32 note: 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/release.asp?ReleaseID=11916.

Window Styles

Window Style Description
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_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 wx.TR_NO_LINES as well.)
wx.TR_LINES_AT_ROOT Use this style to show lines between root nodes. Only applicable if wx.TR_HIDE_ROOT is set and wx.TR_NO_LINES is not set.
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_EXTENDED Use this style to allow disjoint items to be selected. (Only partially implemented; may not work in all cases.)
wx.TR_DEFAULT_STYLE The set of flags that are closest to the defaults for the native control for a particular toolkit.

Event Handling

Event Name Description
wx.EVT_TREE_BEGIN_DRAG(id, func) Begin dragging with the left mouse button.
wx.EVT_TREE_BEGIN_RDRAG(id, func) Begin dragging with the right mouse button.
wx.EVT_TREE_END_DRAG(id, func) End dragging with the left or right mouse button.
wx.EVT_TREE_BEGIN_LABEL_EDIT(id, func) Begin editing a label. This can be prevented by calling Veto.
wx.EVT_TREE_END_LABEL_EDIT(id, func) Finish editing a label. This can be prevented by calling Veto.
wx.EVT_TREE_DELETE_ITEM(id, func) Delete an item.
wx.EVT_TREE_GET_INFO(id, func) Request information from the application.
wx.EVT_TREE_SET_INFO(id, func) Information is being supplied.
wx.EVT_TREE_ITEM_ACTIVATED(id, func) The item has been activated, i.e. chosen by double clicking it with mouse or from keyboard
wx.EVT_TREE_ITEM_COLLAPSED(id, func) The item has been collapsed.
wx.EVT_TREE_ITEM_COLLAPSING(id, func) The item is being collapsed. This can be prevented by calling Veto.
wx.EVT_TREE_ITEM_EXPANDED(id, func) The item has been expanded.
wx.EVT_TREE_ITEM_EXPANDING(id, func) The item is being expanded. This can be prevented by calling Veto.
wx.EVT_TREE_ITEM_RIGHT_CLICK(id, func) The user has clicked the item with the right mouse button.
wx.EVT_TREE_ITEM_MIDDLE_CLICK(id, func) The user has clicked the item with the middle mouse button.
wx.EVT_TREE_SEL_CHANGED(id, func) Selection has changed.
wx.EVT_TREE_SEL_CHANGING(id, func) Selection is changing. This can be prevented by calling Veto.
wx.EVT_TREE_KEY_DOWN(id, func) A key has been pressed.
wx.EVT_TREE_ITEM_GETTOOLTIP(id, func) The opportunity to set the item tooltip is being given to the application (call wx.TreeEvent.SetToolTip). Windows only.
wx.EVT_TREE_ITEM_MENU(id, func) The context menu for the selected item has been requested, either by a right click or by using the menu key.
wx.EVT_TREE_STATE_IMAGE_CLICK(id, func) The state image has been clicked. Windows only.

Control Appearance


wxMSW

wxMSW

wxMAC

wxMAC

wxGTK

wxGTK


Class API

Methods

__init__(parent, id=-1, pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.TR_DEFAULT_STYLE, validator=wx.DefaultValidator, name=wx.TreeCtrlNameStr)

Constructor, creating and showing a tree control.

Parameters:


Returns:

wx.TreeCtrl


AddRoot(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 (PyObject)

Returns:

wx.TreeItemId


AppendItem(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 (PyObject)

Returns:

wx.TreeItemId


AssignImageList(imageList)

Sets the normal 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:

See also

SetImageList.


AssignStateImageList(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:

See also

SetStateImageList.


Collapse(item)

Collapses the given item.

Parameters:


CollapseAll()
Collapses the root item.

See also

ExpandAll


CollapseAllChildren(item)

Collapses this item and all of its children, recursively.

Parameters:


CollapseAndReset(item)

Collapses the given item and removes all children.

Parameters:


Delete(item)

Deletes the specified item. A wx.EVT_TREE_DELETE_ITEM event will be generated. This function may cause a subsequent call to GetNextChild to fail.

Parameters:


DeleteAllItems()
Deletes all items in the control.

Note

Note that this may not generate wx.EVT_TREE_DELETE_ITEM events under some Windows versions although normally such event is generated for each removed item.


DeleteChildren(item)

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

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

Parameters:

Note

Note that this will not generate any events unlike Delete method.


EditLabel(item)

Starts editing the label of the given item. This function generates a wx.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 wx.EVT_TREE_END_LABEL_EDIT event will be sent which can be vetoed as well.

Parameters:


EndEditLabel(cancelEdit)

Ends label editing. If cancelEdit is True, the edit will be cancelled.

Parameters:

  • cancelEdit (bool)

Note

This function is currently supported under Windows only.


EnsureVisible(item)

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

Parameters:


Expand(item)

Expands the given item.

Parameters:


ExpandAll()
Expands all items in the tree.

ExpandAllChildren(item)

Expands the given item and all its children recursively.

Parameters:


GetBoundingRect(item, textOnly)

Retrieves 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.

Parameters:


Returns:

wx.Rect

Note

Notice that the rectangle coordinates are logical, not physical ones. So, for example, the x coordinate may be negative if the tree has a horizontal scrollbar and its position is not 0.


GetChildrenCount(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:


Returns:

int


GetCount()

Returns the number of items in the control.


Returns:

int


GetEditControl()

Returns the edit control being currently used to edit a label. Returns None if no label is being edited.


Returns:

wx.TextCtrl

Note

It is currently only implemented for wxMSW.


GetFirstChild(item)

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

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

Parameters:


Returns:

wx.TreeItemId


GetFirstVisibleItem()

Returns the first visible item.


Returns:

wx.TreeItemId


GetImageList()

Returns the normal image list.


Returns:

wx.ImageList


GetIndent()

Returns the current tree control indentation.


Returns:

int


GetItemBackgroundColour(item)

Returns the background colour of the item.

Parameters:


Returns:

wx.Colour


GetItemFont(item)

Returns the font of the item.

Parameters:


Returns:

wx.Font


GetItemImage(item, which=wx.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:


Returns:

int


GetItemParent(item)

Returns the item’s parent.

Parameters:


Returns:

wx.TreeItemId


GetItemPyData(item)

Returns the tree item data associated with the item. Same as GetPyData

Parameters:


Returns:

PyObject


GetItemText(item)

Returns the item label.

Parameters:


Returns:

string


GetItemTextColour(item)

Returns the colour of the item label.

Parameters:


Returns:

wx.Colour


GetLastChild(item)

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

Parameters:


Returns:

wx.TreeItemId


GetNextChild(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 returned by GetFirstChild and passed to GetNextChild should be the same.

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

Parameters:


Returns:

wx.TreeItemId

See also

GetFirstChild


GetNextSibling(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:


Returns:

wx.TreeItemId

See also

GetPrevSibling


GetNextVisible(item)

Returns the next visible item.

Parameters:


Returns:

wx.TreeItemId


GetPrevSibling(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:


Returns:

wx.TreeItemId


GetPrevVisible(item)

Returns the previous visible item.

Parameters:


Returns:

wx.TreeItemId


GetPyData(item)

Returns the tree item data associated with the item. Same as GetItemPyData.

Parameters:


Returns:

PyObject


GetQuickBestSize()

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.


Returns:

bool

See also

SetQuickBestSize


GetRootItem()

Returns the root item for the tree control.


Returns:

wx.TreeItemId


GetSelection()

Returns the selection, or an invalid item if there is no selection. This function only works with the controls without wx.TR_MULTIPLE style, use GetSelections for the controls which do have this style.


Returns:

wx.TreeItemId


GetSelections()

Returns an array of tree items passed in with the currently selected items. This function can be called only if the control has the wx.TR_MULTIPLE style.


Returns:

list of wx.TreeItemIds


GetSpacing()
No docstrings available for this method.

GetState(node)

Parameters:

Returns:

int


GetStateImageList()

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


Returns:

wx.ImageList


HitTest(pt):

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:

HitTest Flag Description
wx.TREE_HITTEST_ABOVE Above the client area.
wx.TREE_HITTEST_BELOW Below the client area.
wx.TREE_HITTEST_NOWHERE In the client area but below the last item.
wx.TREE_HITTEST_ONITEMBUTTON On the button associated with an item.
wx.TREE_HITTEST_ONITEMICON On the bitmap associated with an item.
wx.TREE_HITTEST_ONITEMINDENT In the indentation associated with an item.
wx.TREE_HITTEST_ONITEMLABEL On the label (string) associated with an item.
wx.TREE_HITTEST_ONITEMRIGHT In the area to the right of an item.
wx.TREE_HITTEST_ONITEMSTATEICON On the state icon for a tree view item that is in a user-defined state.
wx.TREE_HITTEST_TOLEFT To the right of the client area.
wx.TREE_HITTEST_TORIGHT To the left of the client area.

Parameters:

pt (wx.Point): The point at which perform the HitTest calculations


Returns:

(wx.TreeItemId, flags)


InsertItem(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:


Returns:

wx.TreeItemId


InsertItemBefore(parent, before, text, image=-1, selImage=-1, data=None)

Inserts an item after a given one before.

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)
  • before (int)
  • text (string)
  • image (int)
  • selImage (int)
  • data (PyObject)

Returns:

wx.TreeItemId


IsBold(item)

Returns True if the given item is in bold state.

Parameters:


Returns:

bool

See also

SetItemBold


IsEmpty()

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


Returns:

bool


IsExpanded(item)

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

Parameters:


Returns:

bool


IsSelected(item)

Returns True if the item is selected.

Parameters:


Returns:

bool


IsVisible(item)

Returns True if the item is visible (it might be outside the view, or not expanded).

Parameters:


Returns:

bool


ItemHasChildren(item)

Returns True if the item has children.

Parameters:


Returns:

bool


PrependItem(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 (PyObject)

Returns:

wx.TreeItemId


ScrollTo(item)

Scrolls the specified item into view.

Parameters:


SelectItem(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.

Parameters:


SetImageList(imageList)

Sets the normal image list. Image list assigned with this method will not be deleted by wx.TreeCtrl destructor, you must delete it yourself.

Parameters:

See also

AssignImageList


SetIndent(indent)

Sets the indentation for the tree control.

Parameters:

  • indent (int)

SetItemBackgroundColour(item, col)

Sets the colour of the item’s background.

Parameters:


SetItemBold(item, bold=True)

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

Parameters:

See also

IsBold


SetItemDropHighlight(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(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:

See also

SetItemBold


SetItemHasChildren(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(item, image, which=wx.TreeItemIcon_Normal)

Sets the specified item image. See GetItemImage for the description of the which parameter.

Parameters:

See also

GetItemImage


SetItemPyData(item, data)

Sets the item client data. Same as SetPyData.

Parameters:


SetItemText(item, text)

Sets the item label.

Parameters:


SetItemTextColour(item, col)

Sets the colour of the item’s text.

Parameters:


SetPyData(item, data)

Sets the item client data. Same as SetItemPyData.

Parameters:


SetQuickBestSize(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)

See also

GetQuickBestSize


SetSpacing(spacing)

Sets the spacing for the tree control items.

Parameters:

  • spacing (int)

SetState(node, state)

Parameters:


SetStateImageList(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 destructor, you must delete it yourself.

Parameters:


SortChildren(item)

Sorts the children of the given item using OnCompareItems method of wx.TreeCtrl.

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

A simple example of a OnCompareItems method may be:

def OnCompareItems(self, item1, item2):
    """ Overrides OnCompareItems in wx.TreeCtrl. Used by the SortChildren method. """

    # Get the item data
    data1 = GetSomeData(item1)
    data2 = GetSomeData(item2)

    # Compare the item data
    if data1 < data2:
        return -1
    elif data1 > data2:
        return 1
    else:
        return 0

Parameters:

See also

wx.TreeItemData


Toggle(item)

Toggles the given item between collapsed and expanded states.

Parameters:


ToggleItemSelection(item)

Toggles the given item between selected and unselected states. For multiselection controls only.

Parameters:


Unselect(item)

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

Parameters:

  • item (TreeItemId)

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

UnselectItem(item)

Unselects the given item. This works in multiselection controls only.

Parameters:


Properties

Count
See GetCount
EditControl
See GetEditControl
FirstVisibleItem
See GetFirstVisibleItem
ImageList
See GetImageList and SetImageList
Indent
See GetIndent and SetIndent
QuickBestSize
See GetQuickBestSize and SetQuickBestSize
RootItem
See GetRootItem
Selection
See GetSelection
Selections
See GetSelections
Spacing
See GetSpacing and SetSpacing
StateImageList
See GetStateImageList and SetStateImageList