wxPython - Set Items in ListCtrl and Get Selected Item
Question: I have the following code for creating a ListCtrl called "browser list".

    
    
    self.browserList=wx.ListCtrl(panel, pos=(20,150), size=(250,100), style.wx.LC_REPORT|wx.BORDER_SUNKEN)
    self.browserList.InsertColumn(0, '')
    self.browserList.InsertColumn(1, 'Browser: ')
    self.browserList.SetColumnWidth(0, 50)
    self.browserList.SetColumnWidth(1, 200)
    

I wante to add the following to add these strings as items, but it puts it in
the 1st column, whereas I need it in the 2nd column:

    
    
    self.browserList.InsertStringItem(1, 'Google Chrome')
    self.browserList.InsertStringItem(2, 'Mozilla Firefox')
    

Also, how can I get the selected item and store it in a variable?


Answer: It's a bit more complicated than that. You insert the item and then you use
SetStringItem to insert data into other columns. Here's a quick and dirty
example:

    
    
    import wx
    
    ########################################################################
    class MyForm(wx.Frame):
    
        #----------------------------------------------------------------------
        def __init__(self):
            wx.Frame.__init__(self, None, wx.ID_ANY, "List Control Tutorial")
    
            # Add a panel so it looks the correct on all platforms
            panel = wx.Panel(self, wx.ID_ANY)
            self.index = 0
    
            self.list_ctrl = wx.ListCtrl(panel, size=(-1,100),
                                         style=wx.LC_REPORT
                                         |wx.BORDER_SUNKEN
                                         )
            self.list_ctrl.InsertColumn(0, '', width=50)
            self.list_ctrl.InsertColumn(1, 'Browser', width=200)
    
            # add some browsers
            self.list_ctrl.InsertStringItem(0, "foo")
            self.list_ctrl.SetStringItem(0, 1, "Google Chrome")
            self.list_ctrl.InsertStringItem(1, "bar")
            self.list_ctrl.SetStringItem(1, 1, "Mozilla Firefox")
    
            sizer = wx.BoxSizer(wx.VERTICAL)
            sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)
            panel.SetSizer(sizer)
    
    #----------------------------------------------------------------------
    # Run the program
    if __name__ == "__main__":
        app = wx.App(False)
        frame = MyForm()
        frame.Show()
        app.MainLoop()
    

I personally prefer using ObjectListView instead of the ListCtrl. I just think
it works better. But you might find my old tips and tricks tutorial useful:

  * <http://www.blog.pythonlibrary.org/2011/01/04/wxpython-wx-listctrl-tips-and-tricks/>

And if you decide to switch, then you might find this one helpful as well:

  * <http://www.blog.pythonlibrary.org/2009/12/23/wxpython-using-objectlistview-instead-of-a-listctrl/>

