Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将事件绑定到UltimateListCtrl中的复选框?_Python_User Interface_Wxpython - Fatal编程技术网

Python 如何将事件绑定到UltimateListCtrl中的复选框?

Python 如何将事件绑定到UltimateListCtrl中的复选框?,python,user-interface,wxpython,Python,User Interface,Wxpython,我一直在试图弄清楚如何在Python中使用wx.UltimateListCtrl创建定制的小部件。基于一些internet示例,我有这个基本脚本,但我一直在研究如何绑定小部件内的事件,以便在选中第二列中的复选框时从第1列获取stringtext。 代码如下: import sys import wx import wx.lib.agw.ultimatelistctrl as ULC class MyFrame(wx.Frame): def __init__(self, parent)

我一直在试图弄清楚如何在Python中使用wx.UltimateListCtrl创建定制的小部件。基于一些internet示例,我有这个基本脚本,但我一直在研究如何绑定小部件内的事件,以便在选中第二列中的复选框时从第1列获取stringtext。 代码如下:

import sys
import wx
import wx.lib.agw.ultimatelistctrl as ULC

class MyFrame(wx.Frame):

    def __init__(self, parent):

        wx.Frame.__init__(self, parent, -1, "UltimateListCtrl Demo")

        list = ULC.UltimateListCtrl(self, wx.ID_ANY, agwStyle=ULC.ULC_HAS_VARIABLE_ROW_HEIGHT|wx.LC_REPORT|wx.LC_VRULES|wx.LC_HRULES|wx.LC_SINGLE_SEL)
        list.InsertColumn(0, "File Name")
        list.InsertColumn(1, "Select")

        for _ in range(4):
            index = list.InsertStringItem(sys.maxint, "Item " + str(_))
            list.SetStringItem(index, 1, "")
            checkBox = wx.CheckBox( list, wx.ID_ANY, u"", wx.DefaultPosition, wx.DefaultSize, 0 )
            list.SetItemWindow(index, 1, checkBox , expand=True)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(list, 1, wx.EXPAND)
        self.SetSizer(sizer)

        checkBox1.Bind( wx.EVT_CHECKBOX, checkBoxOnCheckBox )

    def __del__( self ):
        pass

    # Virtual event handlers, overide them in your derived class
    def checkBoxOnCheckBox( self, event ):
        print 'Yes'
        #event.Skip()

app = wx.PySimpleApp()  
frame = MyFrame(None)
app.SetTopWindow(frame)
frame.Show()
app.MainLoop()
提前谢谢你的帮助
Ivo

首先:尽量不要命名ULC列表,因为这会屏蔽Python列表

当然,有多种方法来做你想做的事情。一种解决方案是保留复选框的引用,并将其与项目的索引链接。这样您就可以识别项目

我希望这有帮助

import sys
import wx
import wx.lib.agw.ultimatelistctrl as ULC


class MyFrame(wx.Frame):

    def __init__(self, parent):

        wx.Frame.__init__(self, parent, -1, "UltimateListCtrl Demo")

        agwStyle = (ULC.ULC_HAS_VARIABLE_ROW_HEIGHT | wx.LC_REPORT |
                    wx.LC_VRULES | wx.LC_HRULES | wx.LC_SINGLE_SEL)

        self.mylist = mylist = ULC.UltimateListCtrl(self, wx.ID_ANY,
                                                    agwStyle=agwStyle)
        mylist.InsertColumn(0, "File Name")
        mylist.InsertColumn(1, "Select")

        self.checkboxes = {}

        for _ in range(4):
            index = mylist.InsertStringItem(sys.maxint, "Item " + str(_))
            mylist.SetStringItem(index, 1, "")
            checkBox = wx.CheckBox(mylist, wx.ID_ANY, u"", wx.DefaultPosition,
                                   wx.DefaultSize, 0)
            self.checkboxes[checkBox.GetId()] = index
            mylist.SetItemWindow(index, 1, checkBox, expand=True)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(mylist, 1, wx.EXPAND)
        self.SetSizer(sizer)

        self.Bind(wx.EVT_CHECKBOX, self.checkBoxOnCheckBox)

    def __del__(self):
        pass

    # Virtual event handlers, overide them in your derived class
    def checkBoxOnCheckBox(self, event):
        cb = event.GetEventObject()
        idx = self.checkboxes[cb.GetId()]
        print(self.mylist.GetItemText(idx))
        print(cb.GetValue())
        event.Skip()

app = wx.PySimpleApp()  
frame = MyFrame(None)
app.SetTopWindow(frame)
frame.Show()
app.MainLoop()

您的代码帮助我了解了如何处理多个小部件和各自的绑定。非常感谢。