Question

I got a GUI application implemented in wxpython, on the main window, there is a listctrl used to dispaly the names of the files. it was empty at the very beginning. the user clicks the "File", then "open", then chooses a file to open, when this is done by clicking on the "ok" button, the names of the file is supposed to be display in the listctrl. But it seems that this does not work. I used a print clause to check, the print clause works. Here are my codes:

def OnDisplay(self):
    print "On display called"
    self.lc1.InsertStringItem(0, "level 1")
    self.lc1.InsertStringItem(1, "level 2")
    self.lc1.SetBackgroundColour(wx.RED)

    print self.lc1.GetItemText(0)
    print self.lc1.GetItemText(1)

    self.lc1.Refresh()

lc1 is the listctrl, it was initialized at the very beginning when the main window was lauched, but when the OnDisplay was triggered, the print "On display called" works, and the following two print clauses also work. but the listctrl on the main window did not change, i mean, did not show the level 1 and level 2, nor did the background of the listctrl was changed to red, what is the reason please? many thanks!

Was it helpful?

Solution

Here is a runnable example that works on Windows 7, Python 2.6, wx 2.8.

import wx

class ListTest(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, -1, title, size=(380, 230))

        panel = wx.Panel(self, -1)

        self.list = wx.ListCtrl(panel, -1, style=wx.LC_REPORT) 
        self.list.InsertColumn(0, 'col 1', width=140)

        hbox = wx.BoxSizer(wx.HORIZONTAL)
        hbox.Add(self.list, 1, wx.EXPAND)
        panel.SetSizer(hbox)
        self.Centre()
        self.Show(True)

        self.Bind(wx.EVT_CHAR_HOOK, self.onKey)

    def onKey(self, evt):
        if evt.GetKeyCode() == wx.WXK_DOWN:
            self.list.InsertStringItem(0, "level 1")
            self.list.InsertStringItem(1, "level 2")
            self.list.SetBackgroundColour(wx.RED)
            self.list.Refresh()

            print self.list.GetItemText(0)
            print self.list.GetItemText(1)
        else:
            evt.Skip()


app = wx.App()
ListTest(None, 'list test')
app.MainLoop()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top