在wxPython应用程序中实现我自己的事件循环

时间:2014-03-05 23:29:02

标签: multithreading wxpython wxwidgets

我正在编写一个wxPython应用程序,它将进行相当多的数据分析和显示。到目前为止我编写它的方式导致两个线程同时尝试在GUI中更改某些内容时出现问题。我想要做的是设置我自己的simple queue running on the main thread,以便我可以确保一次一个地发生UI更新。

但是,我无法理解如何设置我的事件循环。一般来说,你会做类似

的事情
while True:
    try:
        callback = queue.get(False)
    except Queue.Empty:
        break
    callback()

我认为如果我按原样运行该代码,那么WX将无法做到这一点,因为它永远不会收到任何事件或任何事情,因为控件永远不会离开我的无限循环。如何使这种结构与WX事件循环共存?或者更一般地说,在WX应用程序中,我如何确保某个任务只在主线程上运行?

3 个答案:

答案 0 :(得分:3)

你可以使用wx.callafter,它需要在当前和挂起事件处理程序完成后在guis mainloop中调用的可调用对象。任何额外的位置或关键字args在被调用时都会被传递给callable。

这是一个gui代码示例,它在运行单独的线程并更新主线程中的GUI时利用wx.CallAfter。

代码来自wxpython Phoenix docs

中的Andrea Gavana
#!/usr/bin/env python

# This sample shows how to take advantage of wx.CallAfter when running a
# separate thread and updating the GUI in the main thread

import wx
import threading
import time

class MainFrame(wx.Frame):

    def __init__(self, parent):
        wx.Frame.__init__(self, parent, title='CallAfter example')

        panel = wx.Panel(self)
        self.label = wx.StaticText(panel, label="Ready")
        self.btn = wx.Button(panel, label="Start")
        self.gauge = wx.Gauge(panel)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.label, proportion=1, flag=wx.EXPAND)
        sizer.Add(self.btn, proportion=0, flag=wx.EXPAND)
        sizer.Add(self.gauge, proportion=0, flag=wx.EXPAND)

        panel.SetSizerAndFit(sizer)
        self.Bind(wx.EVT_BUTTON, self.OnButton)

    def OnButton(self, event):
        """ This event handler starts the separate thread. """
        self.btn.Enable(False)
        self.gauge.SetValue(0)
        self.label.SetLabel("Running")

        thread = threading.Thread(target=self.LongRunning)
        thread.start()

    def OnLongRunDone(self):
        self.gauge.SetValue(100)
        self.label.SetLabel("Done")
        self.btn.Enable(True)

    def LongRunning(self):
        """This runs in a different thread.  Sleep is used to
         simulate a long running task."""
        time.sleep(3)
        wx.CallAfter(self.gauge.SetValue, 20)
        time.sleep(5)
        wx.CallAfter(self.gauge.SetValue, 70)
        time.sleep(4)
        wx.CallAfter(self.OnLongRunDone)

if __name__ == "__main__":
    app = wx.App(0)
    frame = MainFrame(None)
    frame.Show()
    app.MainLoop()

答案 1 :(得分:1)

只需将wx.Event发布到某个对象(通常是应用程序或主窗口)。它们将按FIFO顺序处理,尽管它们将与主线程本身发生的其他GUI事件混合在一起。当然,您还需要为这些事件提供实际处理程序,以实现您需要的任何逻辑。

答案 2 :(得分:1)

对于后人,这是我使用Yoriz的答案创建的装饰者。

def run_on_main_thread(fn):
    """Decorator. Forces the function to run on the main thread.

    Any calls to a function that is wrapped in this decorator will
    return immediately; the return value of such a function is not
    available.

    """
    @wraps(fn)
    def deferred_caller(*args, **kwargs):
        # If the application has been quit then trying to use
        # CallAfter will trigger an assertion error via the line
        #     assert app is not None, 'No wx.App created yet'
        # Since assertions are optimized out when -O is used,
        # though, it seems safest to perform the check ourselves,
        # and also catch the exception just in case.
        if wx.GetApp() is not None:
            try:
                wx.CallAfter(fn, *args, **kwargs)
            except AssertionError:
                pass

    return deferred_caller