Question

I have created a wx.ListCtrl and I have filled out the content. How do I retrieve the item with a given string?

I have used FindItem, but it always returns -1 (Not found). Am I using it incorrectly?

print self.List.FindItem(-1, "kid_inst")

>>> -1

I have: wxPython 2.8.10.1, Windows 7, Python 2.4

Was it helpful?

Solution 2

An alternative way to this situation, instead of using FindItem:

When adding items to the ListCtrl, create a dictionary to store the location of each item.

When given the name of the item and when we want to select that in the ListCtrl, use search the location through the dictionary, then use GetItem to get the actual item and finally Select.

item = ListCtrl_name.GetItem(instance_location_dictionary [item_name])
ListCtrl_name.Select(item.GetId())

OTHER TIPS

It seems to work only for the first column:

import wx

class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)
        self.panel = wx.Panel(self)
        self.list = wx.ListCtrl(self.panel, style=wx.LC_REPORT)
        self.list.InsertColumn(0, "No.")
        self.list.InsertColumn(1, "Description")
        self.list.Arrange()

        for i in range(1, 6):
            self.list.Append(["It's %d" % (i), "", ""])
            # DOES NOT WORK! self.list.Append(["", "It's %d" % (i), ""])

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.list, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
        self.panel.SetSizerAndFit(self.sizer)
        self.Show()

        print(self.list.FindItem(-1, "It's 4"))


app = wx.App(False)
win = MainWindow(None)
app.MainLoop()

There is more complex way how to add items to the list by creating wx.ListItem() and SetItemData can be used to add more data to the item. Then you can probably do FindItemData. But I have never done that, so I cannot be of assistance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top