A tree control allows you to programmatically manage the set of selected items in the tree. The basic method for doing this is the method SelectItem(item, select=True). In a single selection tree control, this method takes a wx.Tree-ItemId and makes that item the current selection, and the previously selected item is automatically deselected. If the select parameter is False, then this method can be used to deselect the currently selected item. In a multiple or extended tree control, then the SelectItem() method changes the state of the item without changing the selected status of any other item in the tree. In a multiple or extended selection tree, you can also use the method ToggleltemSelec-tion(item), which just flips the select state of the item passed to the method.
There are three shortcut methods for deselecting items. The plain method Unselect() deselects the currently selected item of a tree in single select mode. In a multiple select tree, use the method UnselectAll(). If you only want to deselect one item within a multiple select tree, use the method UnselectItem(item).
You can query the select state of an item with the method IsSelected(item), which returns Boolean True or False. In a single selection tree, you can get the wx.TreeItemId of the current selection with the method GetSelection(). If it's a multiple selection list, use the method GetSelections(), that returns a Python list of the wx.TreeItemId of all selected items.
When the selection changes in a tree control, there are two events that are triggered that can be captured. The first event, wx.evt_tree_sel_changing, happens before the selection is actually changed. If you handle this event, you can use the event Veto() method to prevent the selection from changing. After the selection has changed, the event wx.evt_tree_sel_changed is triggered. The event class for these events is wx.TreeEvent, discussed more completely in section 15.8.
Was this article helpful?