Search code examples
pythonlistboxwxpythonlive

wxPython update a listbox live


I am developing a programme using python & wxPython. I have a listbox, and I need for it to be updated live to be used as a log.

I have done this simply with the Append() function, but the text added to the listbox is not shown until the end of the procedure, instead of being shown when the Append command is executed. I know this because after each insertion I print the size of the listbox.

def writeLog(self, text):
    self.log.Append(text)
    print self.log.GetStrings().__len__()

Right now, for checking purposes, I am calling a script that has the following code:

parent.writeLog("aaaaaa")
sleep(1)
parent.writeLog("aaaaaa")
sleep(1)
parent.writeLog("aaaaaa")

I have tried these answers but I couldn't make them work for me: Update a ListBox in wxPython wxPython: Update wx.ListBox list

So, How can I see the listBox updated in the screen right after the writeLog function is called? Is it possible? Thanks!


Solution

  • You have a few options here, the easiest perhaps is to call wx.Yield() when you want the ui to be updated, so after your Append calls

    Another solution would be to to get any text that needs adding in a separate thread, and then send it back to the main thread via a custom event or pubsub which can then Append to the listbox