Skip to content Skip to sidebar Skip to footer

How Do You Get Checkbox Selections From A CustomTreeCtrl

I'm working with a CustomTreeCtrl with checkboxes and I can't figure out how to determine which checkboxes are selected. I looked at http://xoomer.virgilio.it/infinity77/wxPython/

Solution 1:

The event object in question doesn't have a GetSelections method. It does have a GetSelection, which will tell you which item was selected at that event. If you want to get all of the selected items inside ItemChecked, rename custom_tree to self.custom_tree and then you're allowed to call self.custom_tree.GetSelections() inside ItemChecked.

If in future you want to know what kind of methods are available for some event object, you can put print(dir(event)) in your handler.

The custom tree control doesn't have a method to get the checked items. One thing that you could do is create a self.checked_items list in your frame, and maintain it in your ItemChecked method. This list could hold either the string values for the items or the items themselves. For instance,

class MyFrame(wx.Frame):
    def __init__(self, parent):
        # ....
        self.checked_items = []
        # ....

    def ItemChecked(self, event):
        if event.IsChecked():
            self.checked_items.append(event.GetItem())
            # or to store the item's text instead, you could do ...
            # self.checked_items.append(self.custom_tree.GetItemText(event.GetItem()))
        else:
            self.checked_items.remove(event.GetItem())
            # or ... 
            # self.checked_items.remove(self.custom_tree.GetItemText(event.GetItem()))

Post a Comment for "How Do You Get Checkbox Selections From A CustomTreeCtrl"