Skip to main content
 首页 » 编程设计

python之wxPython Application.DoEvents() 等效

2025年05月04日23findumars

在 wxPython 中是否有 Application.DoEvents() 等价物?

我正在创建一个表单,然后执行一个缓慢的 I/O 事件,并且在事件完成之前只绘制部分表单。我希望在 I/O 开始之前完全绘制表单。

我试过self.Refresh(),但没有效果。

请您参考如下方法:

wx.Yield或 wx.SafeYield

尽管您确实应该使用单独的线程来执行 I/O 并使用 wx.CallAfter将更新发布到 GUI 线程。

我通常使用这样的模式:

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) 

您可以从 GUI 调用 start_work,例如在 EVT_BUTTON 事件上调用以开始工作。 do_work 在单独的线程上运行,但它不能做任何与 GUI 相关的事情,因为那必须在 GUI 线程上完成。所以你使用 wx.CallAfter 在 GUI 线程上运行一个函数,你可以从工作线程向它传递参数。