如何将事件绑定到wxpython中objectlistview中的复选框

时间:2017-11-03 14:41:52

标签: checkbox wxpython objectlistview-python

我在wxpython中使用了objectlistview,直到现在我对此非常满意。我找不到将事件添加到objectlistview中的复选框的方法。目前我有一个解决方法,用户必须单击一个按钮,然后检查行发生一些事情。但我想在用户选中复选框时实现。它必须在我的情节中切换图形。

我的第二个问题是,在用户点击按钮后,我可以取消选中复选框(如果没有解决我的第一个问题的方法,那就是这样。)

我的代码(我刚刚复制了必要的行,因为我的程序非常大)

self.tempmeasurements = ObjectListView(self, wx.ID_ANY, style=wx.LC_REPORT | wx.SUNKEN_BORDER)
self.tempmeasurements.SetColumns(microanalysis_options.TempMeasColumndefs)
self.tempmeasurements.CreateCheckStateColumn(0)
self.addbutton = wx.Button(self, wx.ID_ANY, "Add to plot")
self.rembutton = wx.Button(self, wx.ID_ANY,'Remove from plot')

self.Bind(wx.EVT_BUTTON, self.on_toggle_plotlist, self.addbutton)
self.Bind(wx.EVT_BUTTON, self.on_remove_from_plot,self.rembutton)

def on_toggle_plotlist(self, event):
    objectsAddPlotList = self.tempmeasurements.GetCheckedObjects()
    pub.sendMessage('MA_ADD_TO_PLOT', Container(origin=self, data=objectsAddPlotList)) #to microanalyse controller
    self.tempmeasurements.SetCheckState(objectsAddPlotList,False)

def on_remove_from_plot(self,event):
    objectsAddPlotList = self.tempmeasurements.GetCheckedObjects()
    pub.sendMessage('MA_REM_FROM_PLOT', Container(origin=self, data=objectsAddPlotList))  # to microanalyse controller

self.tempmeasurements.SetCheckState(objectsAddPlotList,False)行我试图在用户点击按钮后取消选中复选框。

这就是列表的样子:

list with checkboxes

1 个答案:

答案 0 :(得分:1)

关键是要导入OLVEvent,然后将ObjectListView个实例绑定到OLVEvent.EVT_ITEM_CHECKED

我继续创造了一个简单的例子:

import wx

from ObjectListView import ObjectListView, ColumnDefn, OLVEvent


class Results(object):
    """"""

    def __init__(self, tin, zip_code, plus4, name, address):
        """Constructor"""
        self.tin = tin
        self.zip_code = zip_code
        self.plus4 = plus4
        self.name = name
        self.address = address


class MyPanel(wx.Panel):
    """"""

    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent=parent)

        mainSizer = wx.BoxSizer(wx.VERTICAL)

        self.test_data = [Results("123456789", "50158", "0065", "Patti Jones",
                                  "111 Centennial Drive"),
                          Results("978561236", "90056", "7890", "Brian Wilson",
                                  "555 Torque Maui"),
                          Results("456897852", "70014", "6545", "Mike Love", 
                                  "304 Cali Bvld")
                          ]
        self.results_olv = ObjectListView(self, 
                                         style=wx.LC_REPORT|wx.SUNKEN_BORDER)
        self.results_olv.Bind(OLVEvent.EVT_ITEM_CHECKED, self.on_item_checked)

        self.set_results()

        mainSizer.Add(self.results_olv, 1, wx.EXPAND|wx.ALL, 5)
        self.SetSizer(mainSizer)

    def on_item_checked(self, event):
        obj = self.results_olv.GetSelectedObject()
        checked = 'Checked' if self.results_olv.IsChecked(obj) else 'Unchecked'
        print('{} row is {}'.format(obj.name, checked))

    def set_results(self):
        """"""
        self.results_olv.SetColumns([
            ColumnDefn("TIN", "left", 100, "tin"),
            ColumnDefn("Zip", "left", 75, "zip_code"),
            ColumnDefn("+4", "left", 50, "plus4"),
            ColumnDefn("Name", "left", 150, "name"),
            ColumnDefn("Address", "left", 200, "address")
        ])
        self.results_olv.CreateCheckStateColumn()
        self.results_olv.SetObjects(self.test_data)


class MainFrame(wx.Frame):
    """"""

    def __init__(self):
        """Constructor"""
        title = "OLV Checkbox Tutorial"
        wx.Frame.__init__(self, parent=None, title=title, 
                          size=(600, 400))
        panel = MyPanel(self)


if __name__ == "__main__":
    app = wx.App(False)
    frame = MainFrame()
    frame.Show()
    app.MainLoop()