0

我在 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) 行取消选中复选框。

这是列表的样子:

带有复选框的列表

4

1 回答 1

2

关键是导入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()
于 2017-11-09T17:24:53.080 回答