如何从 CustomTreeCtrl 获取复选框选择 [英] How do you get checkbox selections from a CustomTreeCtrl

查看:32
本文介绍了如何从 CustomTreeCtrl 获取复选框选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有复选框的 CustomTreeCtrl,但我不知道如何确定选择了哪些复选框.我看了 http://xoomer.virgilio.it/infinity77/wxPython/Widgets/wx.TreeCtrl.html#GetSelection 并将它们放在一起:

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()

当我选中一个框时,我得到了回溯:AttributeError: 'TreeEvent' object has no attribute 'GetSelections'" 关于如何读取哪些框被选中的任何建议都会很棒!

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!

推荐答案

有问题的 event 对象没有 GetSelections 方法.它确实有一个 GetSelection,它会告诉您在该事件中选择了哪个项目.如果您想获取 ItemChecked 中的所有选定项目,请将 custom_tree 重命名为 self.custom_tree,然后您就可以调用 ItemChecked 内的 >self.custom_tree.GetSelections().

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.

如果以后你想知道某个事件对象有哪些可用的方法,你可以把 print(dir(event)) 放在你的处理程序中.

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.

自定义树控件没有获取选中项的方法.您可以做的一件事是在您的框架中创建一个 self.checked_items 列表,并在您的 ItemChecked 方法中维护它.此列表可以包含项目的字符串值或项目本身.例如,

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()))

这篇关于如何从 CustomTreeCtrl 获取复选框选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆