checkboxwxpythonobjectlistview-python

How to bind an event to the checkbox in the objectlistview in wxpython


I'm using objectlistview in wxpython and I am very happy with it until now. I cannot find a way to add an event to the checkbox in my objectlistview. At the moment I have a workaround, the user have to click on a button and then something happens with the checked row. But I would like to make it happen when the user checks the checkbox. It have to toggle a graph in my plot.

A second question I have is how I can uncheck the checkboxes after the user clicked the button (this is for if there isn't a way to solve my first question).

My code (I just copied the necessary lines, because my program is very big)

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

The self.tempmeasurements.SetCheckState(objectsAddPlotList,False) line I tried to use to uncheck the checkboxes after the user clicked the button.

this is how the list looks like:

list with checkboxes


Solution

  • The key thing is to import OLVEvent and then bind your ObjectListView instance to OLVEvent.EVT_ITEM_CHECKED.

    I went ahead and created a simple example:

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