Q:

how to get the information of an item in wxListCtrl?

I have a list generated using wxListCtrl where it has three coloumns. The data generated when the list updates is in need to use in the other part of my code.Can any one please tell me how could i get the all values of an item of all 3 coloumns in the list?
My list is as below...
self.list_ctrl = wx.ListCtrl(self.panel, size=(565,150),pos=(15,20),style=wx.LC_REPORT | wx.BORDER_SUNKEN)
self.name=self.list_ctrl.InsertColumn(0, 'Task Name',width=189)
self.date=self.list_ctrl.InsertColumn(1, 'Run ',width=189)
self.status=self.list_ctrl.InsertColumn(2, 'Status', width=187
self.index=0

where the items are generated using..
Taskname=self.list_ctrl.InsertStringItem(self.index,task)
Taskdate=self.list_ctrl.SetStringItem(self.index, 1,strftime("%d-%m-%Y", gmtime()))
Tasktime=self.list_ctrl.SetStringItem(self.index,2,datetime.now().strftime('%H:%M:%S'))

I can able to get the name of the item i.e, 'self.name' which is under 1st coloumn using
name=self.list_ctrl.GetItemText(self.name)

But 'self.date' and 'self.time' are returning the int type values.How can i get the date and time in the variable 'Taskdate' and 'Tasktime' respectively?

A:

There are a couple of ways to do this. The simplest (in my opinion) is to associate objects with each row, but we'll do it the "hard" way first:
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, 'Subject')
        self.list_ctrl.InsertColumn(1, 'Due')
        self.list_ctrl.InsertColumn(2, 'Location', width=125)

        btn = wx.Button(panel, label="Add Line")
        btn2 = wx.Button(panel, label="Get Data")
        btn.Bind(wx.EVT_BUTTON, self.add_line)
        btn2.Bind(wx.EVT_BUTTON, self.getColumn)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)
        sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
        sizer.Add(btn2, 0, wx.ALL|wx.CENTER, 5)
        panel.SetSizer(sizer)

    #----------------------------------------------------------------------
    def add_line(self, event):
        line = "Line %s" % self.index
        self.list_ctrl.InsertStringItem(self.index, line)
        self.list_ctrl.SetStringItem(self.index, 1, "01/19/2010")
        self.list_ctrl.SetStringItem(self.index, 2, "USA")
        self.index += 1

    #----------------------------------------------------------------------
    def getColumn(self, event):
        """"""
        count = self.list_ctrl.GetItemCount()
        cols = self.list_ctrl.GetColumnCount()
        for row in range(count):
            for col in range(cols):
                item = self.list_ctrl.GetItem(itemId=row, col=col)
                print item.GetText()

#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

This is slightly modified from an earlier answer to a similar question. Anyway, let's take a look at how to use objects instead:
import wx

########################################################################
class Car(object):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, make, model, year, color="Blue"):
        """Constructor"""
        self.make = make
        self.model = model
        self.year = year
        self.color = color

########################################################################
class MyPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)

        rows = [Car("Ford", "Taurus", "1996"),
                Car("Nissan", "370Z", "2010"),
                Car("Porche", "911", "2009", "Red")
                ]

        self.list_ctrl = wx.ListCtrl(self, size=(-1,100),
                                style=wx.LC_REPORT
                                |wx.BORDER_SUNKEN
                                )
        self.list_ctrl.Bind(wx.EVT_LIST_ITEM_SELECTED, self.onItemSelected)
        self.list_ctrl.InsertColumn(0, "Make")
        self.list_ctrl.InsertColumn(1, "Model")
        self.list_ctrl.InsertColumn(2, "Year")
        self.list_ctrl.InsertColumn(3, "Color")

        index = 0
        self.myRowDict = {}
        for row in rows:
            self.list_ctrl.InsertStringItem(index, row.make)
            self.list_ctrl.SetStringItem(index, 1, row.model)
            self.list_ctrl.SetStringItem(index, 2, row.year)
            self.list_ctrl.SetStringItem(index, 3, row.color)
            self.myRowDict[index] = row
            index += 1

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)
        self.SetSizer(sizer)

    #----------------------------------------------------------------------
    def onItemSelected(self, event):
        """"""
        currentItem = event.m_itemIndex
        car = self.myRowDict[currentItem]
        print car.make
        print car.model
        print car.color
        print car.year

########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, wx.ID_ANY, "List Control Tutorial")
        panel = MyPanel(self)
        self.Show()

#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()

Here we create a list of Car objects and we use dot-notation to add properties of the class to the ListCtrl. Then when we select an item from the list, we grab the currently selected item from the event object and look it up using a dictionary. Not exactly simple, but I rather like it. You can read more about it along with other tips and tricks here.
However, I think the best solution is to just use ObjectListView (a ListCtrl wrapper) that makes the rows into true objects and allows much easier access to their values, as well as bringing in a bunch of other enhancements. Sadly it's not a part of the normal wxPython distribution as of yet, but it's easy to add from PyPI. You can read more about it in this article from blog too!

