How do I add a root

When you add items to the tree, the first item you add must be the root element. The method for adding the tree's root is AddRoot(), called with the following parameters:

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

You can add a root only once. If you try to add a second root after one has been set, wxPython raises an exception. The text parameter of the method contains the display string for the root element. The image parameter is an index into the image list representing the image displayed next to the text in the tree. The image lists will be discussed in more detail in section 15.5, but for now it's enough to know that they behave similarly to the image list for a list control. The sel-Image is a similar index that is set into the image list for selected items. The data parameter is a data object that is associated with that item, mostly for purposes of sorting. However, as a wxPython programmer you do not want to use this mechanism for specifying the data object, because the C++ data type of the data parameter is a class called wx.TreeItemData. If you are programming in C + +, use wx.TreeItemData as a wrapper around your data. If you are programming in Python, there's a shortcut that we'll discuss in section 15.3.

The AddRoot() method returns an ID for the root item. The tree control uses its own class to manage items, wx.TreeItemId. For the most part, you don't need to worry about the exact value of this ID. It's enough to know that each item has a unique wx.TreeItemId, and that the values can be tested for equality. The methods of the tree control class that take indexes to search or identify individual items of the tree all take instances of wx.TreeItemId. The wx.TreeItemId is not mapped to any simple type—its actual data value is not relevant, since you only use it to test for equality.

Was this article helpful?

0 0

Post a comment