Python:如何显示对话框窗口并同时工作

标签 python multithreading wxpython

考虑一下:

import wx, time

# STEP 1: Setup Window
class Window(wx.Frame):
    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Bobby the Window', size=(300,200))
        panel = wx.Panel(self)
        self.Bind(wx.EVT_CLOSE, self.closewindow)

    wx.StaticText(panel, -1, "Yo user, the program is busy doing stuff!", (50,100), (200,100))

def closewindow(self, event):
    self.Destroy()

# STEP 2: Show Window (run in background, probably with threading, if that is the best way)
if __name__=='__main__':
    app = wx.PySimpleApp()
    frame = Window(parent=None,id=-1)
    frame.Show()
    app.MainLoop()

# STEP 3: Do work
time.sleep(5)

# STEP 4: Close Window, and show user evidence of work
## *Update window to say: "I am done, heres what I got for you: ^blah blah info!^"*
exit()

我的问题是:

  • 在第 4 步中,如何更改窗口中的文本(尤其是在线程中)?
  • 在步骤 2 中,如何在后台运行窗口,但仍然能够与其通信? (更新文字等)

这类似于我关于如何运行 cli 进度条并同时工作的问题,除了 GUI 窗口之外。

我知道要更改 StaticText,我会执行“text.SetLabel("BLAH!")”,但是如果它在后台运行,我将如何与窗口类进行通信?

更新: This thread也有一些帮助。

最佳答案

如果您需要执行长时间运行的进程,那么您将必须使用某种类型的线程或者多处理模块。否则,您将阻塞 GUI 的主循环并使其无响应。您还需要使用 wxPython 的线程安全方法从线程与 GUI 进行通信。它们是wx.CallAfter、wx.CallLater 和wx.PostEvent。

您可以在以下位置阅读有关 wxPython 和线程的更多信息:

关于Python:如何显示对话框窗口并同时工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13736478/

相关文章:

python - 如何在 Windows 上获取 uwsgi

python - 为什么 Pylint 不喜欢内置函数?

python - 计算 numpy 矩阵的极分解?

c++ - 如何在 Qt 中等待线程完成而不阻塞其执行?

android - 整个应用程序生命周期中的 getWritableDatabase() 实例

python - 如何向 ObjectListView wxPython 添加仪表或进度条?

python - (Python) 如何测试文件中是否包含字符串

javascript - "Isolate"在V8中的作用是什么?怎么可能单独制作 "Isolate"呢?

python - wxpython 构建器类似于 boa 构造器

python - 我如何检测我的窗口何时被 wxPython 最小化?