wxPython 状态栏临时文本

0 投票
1 回答
795 浏览
提问于 2025-04-18 03:32

如何正确地在状态栏中设置临时文本,同时要知道图形界面的操作需要在主线程中进行?

这是我目前的解决方案,使用了另一个线程

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()

1 个回答

3

我会使用一个叫做 wx.Timer() 的对象。你可以设置这个计时器,让它在 X 毫秒后触发,然后和它关联的事件就会执行,这样你就可以清空状态栏的文字,并且停止计时器。

所以你可以这样做:

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

然后在你的处理函数里,你可以这样做:

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

你可以在以下链接中了解更多:

撰写回答