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/Widgets/wx.TreeCtrl.html#GetSelection and put this together:
import string
import os
import sys
import wx
import wx.lib.agw.customtreectrl as CT
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "CustomTreeCtrl Demo")
custom_tree = CT.CustomTreeCtrl(self, agwStyle=wx.TR_DEFAULT_STYLE)
root = custom_tree.AddRoot("The Root Item")
for y in range(5):
last = custom_tree.AppendItem(root, "item %d" % y)
for z in range(5):
item = custom_tree.AppendItem(last, "item %d" % z, ct_type=1)
self.Bind(CT.EVT_TREE_ITEM_CHECKED, self.ItemChecked)
def ItemChecked(self, event):
print("Somebody checked something")
print(event.GetSelections())
app = wx.PySimpleApp()
frame = MyFrame(None)
app.SetTopWindow(frame)
frame.Show()
app.MainLoop()
When I check a box, I get the Traceback: “AttributeError: ‘TreeEvent’ object has no attribute ‘GetSelections'” Any suggestions on how to read which boxes are selected would be great!
The
eventobject in question doesn’t have aGetSelectionsmethod. It does have aGetSelection, which will tell you which item was selected at that event. If you want to get all of the selected items insideItemChecked, renamecustom_treetoself.custom_treeand then you’re allowed to callself.custom_tree.GetSelections()insideItemChecked.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_itemslist in your frame, and maintain it in yourItemCheckedmethod. This list could hold either the string values for the items or the items themselves. For instance,