Search code examples
pythonwxpythonlistctrl

Use arbitrary wx objects as a column in a wx.ListCtrl


I have a wx.ListCtrl that has the wx.LC_REPORT bit set. It has 3 columns. I want the first column to be populated with a check box for each other entry. I tried using the ListCtrl.InsertItem method, but it only takes one argument (info) and I can't find any docs as to what that argument needs to be. I've tried just passing a wx.CheckBox to InsertItem to no avail.

Is it possible to have a checkbox as an entry in a wxPython ListCtrl? If so, how would I go about doing that?

In case there's any ambiguity as to what I'm talking about, here's a picture of what I want (not sure if this is wx, but it's what I'm looking for). I want the checkboxes next to 1..5 in the No. column.

list control with checkboxes


Solution

  • Have a look at wx.lib.mixins.listctrl.

    import wx
    import wx.lib.mixins.listctrl as listmix
    
    class TestListCtrl(wx.ListCtrl, listmix.CheckListCtrlMixin, listmix.ListCtrlAutoWidthMixin):
        def __init__(self, *args, **kwargs):
            wx.ListCtrl.__init__(self, *args, **kwargs)
            listmix.CheckListCtrlMixin.__init__(self)
            listmix.ListCtrlAutoWidthMixin.__init__(self)
            self.setResizeColumn(3)
    
        def OnCheckItem(self, index, flag):
            print(index, flag)
    
    class MainWindow(wx.Frame):
        def __init__(self, *args, **kwargs):
            wx.Frame.__init__(self, *args, **kwargs)
            self.panel = wx.Panel(self)
            self.list = TestListCtrl(self.panel, style=wx.LC_REPORT)
            self.list.InsertColumn(0, "No.")
            self.list.InsertColumn(1, "Progress")
            self.list.InsertColumn(2, "Description")
            self.list.Arrange()
            for i in range(1, 6):
                self.list.Append([str(i), "", "It's the %d item" % (i)])        
            self.button = wx.Button(self.panel, label="Test")
            self.sizer = wx.BoxSizer(wx.VERTICAL)
            self.sizer.Add(self.list, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
            self.sizer.Add(self.button, flag=wx.EXPAND | wx.ALL, border=5)
            self.panel.SetSizerAndFit(self.sizer)
            self.Show()
    
    app = wx.App(False)
    win = MainWindow(None)
    app.MainLoop()