How do I add more items to the tree

Once you have the root in place, you can begin to add elements to the tree. The method you'll use most frequently is AppendItem(parent, text, image=-1, sel-Image=-1, data=None) . The parent parameter is the wx.TreeItemId of the existing tree item which will be the parent of the new item. The text is the text string that is displayed for the new item. The image and selImage parameters are, as in the add root method, indexes into the appropriate image list for the image to be displayed with the new item. The data parameter is also used the same way as in the add root method. The result of this method is that the new item is placed at the end of the parent item's list of children. The method returns the wx.Tree-ItemId of the newly created item. You'll need to hold on to that ID if you want to add children to that item. A sample interaction may look like the following.

rootId = tree.AddRoot("The Root")

childId = tree.AppendItem(rootId, "A Child")

grandChildId = tree.AppendItem(childId, "A Grandchild")

This snippet provides you with a root item, gives the root item a child, and gives that child a child node of its own.

To add the child item at the beginning of the list of children rather than at the end, use the method PrependItem(parent, text, image=-1, selImage=-1, data= None). With the exception of the placement of the new item, this method behaves identically to AppendItem(). The parameters have the same meaning, and it also returns the wx.TreeItemId of the new item.

If you want to insert an item into an arbitrary spot of your tree, you can use one of the two insert methods. The first is InsertItem(parent, previous, text, image=-1, selImage=-1, data=None). The parent, text, image, selImage, and data parameters are identical to the append and prepend versions of this method. The previous parameter is a wx.TreeItemId corresponding to an item in the child list of the parent. The new item is placed in that child after the item passed in the previous argument.

If you want to put a new item before something in the list rather than after, use the method InsertItemBefore(parent, before, text, image=-1, selImage=-1, data=None) . As the name suggests, this method places the new item before the item passed in the before parameter. However, the before parameter is not a tree item ID. Rather, it is the integer index of the item in the child list. (Yes, this is confusing. No, there's no easy way to get at the index, you have to manage it yourself. Yes, this is the legacy of a rough patch in the original C + + code.) In any case, assuming you've passed in the correct value, this behaves as you would expect. The new item is added before the item at the given index, and the method returns a wx.TreeItemId for the new item.

Was this article helpful?

0 0

Post a comment