wxPython:在其他线程中执行时显示框架

1 投票
1 回答
647 浏览
提问于 2025-04-18 04:54

在我的图形用户界面(GUI)中,我使用wxPython进行一些可能需要花费时间的计算。所以我想在一个单独的线程中启动这些计算,并在GUI中显示一个窗口,告诉用户程序正在计算。在这个过程中,主窗口应该被禁用。

这是我的代码:

import time
import threading

import wx

def calculate():
    # Simulates the calculation
    time.sleep(5)
    return True

class CalcFrame(wx.Frame):

    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent=None, id=-1, title="Calculate")
        # Normally here are some intctrls, but i dont show them to keep it easy
        self.panel = wx.Panel(parent=self, id=-1)
        self.createButtons()

    def createButtons(self):
        button = wx.Button(parent=self.panel, id=-1, label="Calculate")
        button.Bind(wx.EVT_BUTTON, self.onCalculate)

    def onCalculate(self, event):
        calcThread = threading.Thread(target=calculate)
        checkThread = threading.Thread(target=self.checkThread, args=(calcThread,))
        self.createWaitingFrame()
        self.waitingFrame.Show(True)
        self.Disable()
        calcThread.run()
        checkThread.run()

    def createWaitingFrame(self):
        self.waitingFrame = wx.MiniFrame(parent=self, title="Please wait")
        panel = wx.Panel(parent=self.waitingFrame)
        waitingText = wx.StaticText(parent=panel, label="Please wait - Calculating", style=wx.ALIGN_CENTER)

    def checkThread(self, thread):
        while thread.is_alive():
            pass
        print "Fertig"
        self.waitingFrame.Destroy()
        self.Enable()

app = wx.PySimpleApp()
frame = CalcFrame(parent=None, id=-1)
frame.Show()
app.MainLoop()

但是我现在的问题是,当我按下“计算”按钮时,等待窗口没有正确显示,我看不到文本。而且我也无法移动、最大化或最小化主窗口。

你能帮我吗?提前谢谢你 :)

1 个回答

1

你绝对不应该在主线程以外的线程中更新图形界面(GUI)……我很确定wxPython的文档在多个地方提到过这一点……它不是线程安全的,这样会导致你的图形界面出现问题。

相反,我认为你应该这样做:

def thread1():
     time.sleep(5)
     return True

def thread2(t1,gui):
    while thread.is_alive():
        pass
    print "Fertig"
    wx.CallAfter(gui.ThreadDone)

class MyFrame(wx.Frame):
   def startThread(self):
      calcThread = threading.Thread(target=thread1)
      checkThread = threading.Thread(target=thread2, args=(calcThread,self))
   def ThreadDone(self):
       print "Both threads done???"
       print "Now modify gui from main thread(here!)"

撰写回答