How to properly set temporary text in statusbar, knowing that GUI operations need to take place in the main thread?

This is my current solution, using another thread

class StatusBar(wx.StatusBar):
    def __init__(self, parent):
        super(StatusBar, self).__init__(parent)

    def set_status(self, s, pause=None):
        if pause is None:
            self.SetStatusText(s)
        else:
            def aux():
                self.SetStatusText(s)
                time.sleep(pause)
                if self.GetStatusText() == s:
                    self.SetStatusText("")
            threading.Thread(target=aux).start()
有帮助吗?

解决方案

I would use a wx.Timer() object. You can set the timer to run for X number of milliseconds and then the event it is attached to will fire and you can clear the status bar's text and stop the timer too.

So you'd do something like this:

self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.clearStatus, self.timer)
self.timer.Start(10000)  # fire in 10 seconds

Then in your handler, you'd do something like this:

def clearStatus(self, event):
    self.SetStatusText("")
    self.timer.Stop()

You can read more at the following:

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top