python - wxPython Application.DoEvents() 等效?

标签 python wxpython wxwidgets

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

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

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

最佳答案

wx.Yieldwx.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 线程上运行一个函数,你可以从工作线程向它传递参数。

关于python - wxPython Application.DoEvents() 等效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2342183/

相关文章:

c++ - wxWidgets 关闭时出现段错误

python - 强制变量为整数 CVXPY

python - 如何调用self方法并获取flask中的数据?

python - wxpython:如何在flexgridsizer中保持三个wxTextCtrl具有相同的大小

python - 带有 sizer 的 wxpython 布局

c++ - 如何让自定义小部件接受像 textctrl 这样的键盘事件

python - 从字典创建 1 行数据框,包含三列并将变量附加到第一列

python - 如何在 for 循环中使用多重处理?如何让一个核心运行从索引n1到n2的循环,另一个核心从索引n2到n3运行循环?

python - wxPython TextCtrl断言错误: wx. wxEVT_COMMAND_TEXT_ENTER不是PyEventBinder实例

OpenCV 在不同机器上加载视频文件时出错