Search code examples
iowxpythonlistctrl

python print contents from wx.ListCtrl


I have a list created as

self.statusListCtrl = wx.ListCtrl(self.panelUpper, -1, style=wx.LC_REPORT|wx.SUNKEN_BORDER)

I add data to this list using

self.statusListCtrl.Append([datetime.datetime.now(),action,result])

When my process is all done I have a nice list showing things that were tried, the result of that attempt and a datetime stamp. Now what I want to do is output that to a text file. my problem is that I cant get the data from my listctrl correctly.

This is how I am trying to iterate through the list.

fileName = 'd:\ssplogs\sspInstaller.log'
FILE = open(fileName,"w")

for itemIdx in range(0,self.statusListCtrl.GetItemCount()):
    line = str(self.statusListCtrl.GetItemText(itemIdx) + "\n")            
    print "line" , line        
    writeLine = line
    FILE.write(writeLine)

FILE.close()

The output I am getting though is only my datetime stamp, which is the first column of my list. How do I get this so I see something like

datetime, action, result

Solution

  • Use this to get Row x Column data:

    self.list.GetItem(row, col).GetText()
    

    Working example:

    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, "Date")
            self.list.InsertColumn(1, "Action")
            self.list.InsertColumn(2, "Result")
    
            for a in range(5):        
                self.list.Append(["Date %d" % a, "Action %d" % a, "Result %d" % a])
    
            self.sizer = wx.BoxSizer()
            self.sizer.Add(self.list, proportion=1, flag=wx.EXPAND)
    
            self.panel.SetSizerAndFit(self.sizer)
            self.Show()
    
            # Save
            for row in range(self.list.GetItemCount()):
                print(", ".join([self.list.GetItem(row, col).GetText() for col in range(self.list.GetColumnCount())]))
    
    app = wx.App(False)
    win = MainWindow(None)
    app.MainLoop()