Summary

■ The tree control provides you with a compact display of nested, hierarchical data, such as a file tree, or an XML document. Tree controls are instances of the class wx.TreeCtrl. Occasionally, you'll want to subclass wx.TreeCtrl, particularly if you need to do custom sorting.

■ To add items to the tree, start with the method AddRoot(text, image=-l, selImage=-1, data=None). The return value of this method is a wx.TreeItemId representing the root item of the tree. The tree control uses its wx.TreeItemId as its own identifier class, rather than using integer IDs, as most of the other widgets do. Once you have the root item, you can start adding child items with the method AppendItem(parent, text, image=-1, sellmage=-l, data=None), passing it the ID of the parent item. The new item is added to the end of the child list of the parent item. The method returns the wx.TreeItemId of the new item. There are related items for prepending the item to the front of the list, or inserting it at an arbitrary location within the list. The method Delete(item) removes an item from the tree, and DeleteChildren(item) removes all the children of the given item.

■ The tree control has several styles which change the visual display of the tree. One set controls the type of flip button next to the item for expanding or collapsing the tree item. Another set controls whether lines are drawn between items in the list. A third set controls whether the tree has single or multiple selection. You can also use styles to simulate a tree with multiple roots by hiding the actual root of the tree.

■ By default, a tree can be sorted in alphabetical order of the display text. However, in order for this to work, you must attach data to each item. The easiest way to do this is with the method SetltemPyData(item, obj) which associates an arbitrary Python object with the item. You can recover the data with GetltemPyData(item). If you want to use the data to write a custom sorting function, you must extend the wx.TreeCtrl class and override the method OnCompareltems(item1, item2) where the two arguments are the IDs of the items to be compared.

■ The tree control manages images with an image list similar to the way that list controls manage it. You use SetlmageList(imageList) or Assignlmage-List(imageList) to attach an image list to the tree control. Then, when new items are added to the list, you can associate them with specific indexes within the image list.

■ There is no specific method that allows you to recover the child list of a parent item. Instead, you need to iterate over the child list, starting with the method GetFirstChild(item). This returns the item ID of the first child along with a cookie that you use to pass to GetNextChild(item, cookie) and which allows multiple iterations of the child list to be active at the same time. You can also go straight to the end of the list with GetLastChild(item).

■ You can manage the selection of the tree with the method Select-Item(item, select=True). In a multiple selection tree, you can also use ToggleltemSelection(item) to change the state of the given item. You can query the state of an item with isSelected(item). You can expand or collapse an item with Expand(item) or Collapse(item), or toggle its current state with Toggle(item).

■ The style wx.tr_edit_labels makes the tree control user editable. In an editable list, the user can select an item, and type in a new display label. Pressing escape cancels the edit without changing the item. You can also veto the edit by trapping the wx.evt_tree_end_label_edit event type. Vetoing the event forces a cancel without changing the item. The wx.TreeEvent class provides properties that allow access to the display text of the item being manipulated, as well as other properties of the event.

Was this article helpful?

0 0

Post a comment