简体   繁体   English

wxPython Application.DoEvents()是否等效?

[英]wxPython Application.DoEvents() equivalent?

Is there an Application.DoEvents() equivalent in wxPython? wxPython中是否有等效的Application.DoEvents()?

I am creating a form, then doing a slow I/O event, and the form is only partially drawn until the event finishes. 我正在创建一个表单,然后执行一个缓慢的I / O事件,并且仅在事件完成之前才部分绘制该表单。 I'd like to have the form fully drawn before the I/O starts. 我想在I / O开始之前完整绘制表格。

I've tried self.Refresh() , but it has no effect. 我已经尝试过self.Refresh() ,但是没有效果。

wx.Yield or wx.SafeYield wx.Yieldwx.SafeYield

Although you should really use a separate thread to do the I/O and use wx.CallAfter to post updates to the GUI thread. 尽管您实际上应该使用单独的线程来执行I / O,并使用wx.CallAfter将更新发布到GUI线程。

I usually use a pattern like this: 我通常使用这样的模式:

def start_work(self):
    thread = threading.Thread(target=self.do_work, args=(args, go, here))
    thread.setDaemon(True)
    thread.start()
def do_work(self, args, go, here):
    # do work here
    # wx.CallAfter will call the specified function on the GUI thread
    # and it's safe to call from a separate thread
    wx.CallAfter(self.work_completed, result, args, here)
def work_completed(self, result, args, here):
    # use result args to update GUI controls here
    self.text.SetLabel(result)

You would call start_work from the GUI, for example on an EVT_BUTTON event to start the work. 您可以从GUI调用start_work ,例如在EVT_BUTTON事件上启动工作。 do_work is run on a separate thread but it cannot do anything GUI related because that has to be done on the GUI thread. do_work在单独的线程上运行,但是它不能执行与GUI相关的任何操作,因为必须在GUI线程上完成。 So you use wx.CallAfter to run a function on the GUI thread, and you can pass it arguments from the work thread. 因此,您可以使用wx.CallAfter在GUI线程上运行一个函数,并且可以从工作线程传递该参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM