Search code examples
pythoncheckboxwxpython

wxPython UltimateListCtrl check (tick) listitem programatically


I am using UltimateListCtrl with LC_LIST style. I have all items in list with checkboxes. After populating list, I am trying to check (tick) some items dynamically as per the data I receive from database. But I am not able to check (tick) any item in the list after the list is populated.

Here I am pasting sample code for describing the issue.

More Info: I am using Python 2.7 and wxPython 2.8 on platform Windows XP.

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

class MyFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, parent=None, title="Tick List Item Checkbox Programatically")
        self.panel = wx.Panel(parent=self)
        self.sizer = wx.BoxSizer(orient=wx.VERTICAL)
        self.panel.SetSizer(self.sizer)

        self.list = ULC.UltimateListCtrl(parent=self.panel, agwStyle=wx.LC_LIST)
        self.sizer.Add(item=self.list, proportion=1, flag=wx.EXPAND)

        for i in range(50):
            label = "Item %02d " % (i + 1) + "random " * random.randint(1, 3)
            self.list.InsertStringItem(index=i, label=label, it_kind=1)

        self.checkItems()

        self.Show()


    def checkItems(self):
        idx = [1, 3, 6, 7, 9, 11, 15, 17, 20]
        for i in idx:
            # self.list.SetItemState(item=i, state=ULC.ULC_STATE_SELECTED, stateMask=ULC.ULC_MASK_CHECK)
            # self.list.SetItemState(item=i, state=ULC.ULC_STATE_SELECTED, stateMask=ULC.ULC_MASK_STATE)
            self.list.SetItemState(item=i, state=ULC.ULC_STATE_SELECTED, stateMask=ULC.ULC_STATE_SELECTED)


if __name__ == '__main__':
    app = wx.App()
    frame = MyFrame()
    app.MainLoop()

How can I check (tick) list item?


Solution

  • You need to get the item from the list, check it and then save it back into the list:

    def checkItems(self):
        idx = [1, 3, 6, 7, 9, 11, 15, 17, 20]
        for i in idx:
            temp = self.list.GetItem(i, 0)
            temp.Check(True)
            self.list.SetItem(temp)