wxPython:在其他线程中执行某些操作时显示帧

wxPython:在其他线程中执行某些操作时显示帧,python,multithreading,wxpython,Python,Multithreading,Wxpython,在我的图形用户界面与wxPython如果必须做一些计算,这可能需要一些时间。所以我想在一个单独的线程中启动它们,并在GUI中显示一个窗口,显示程序正在计算的内容。在此期间,应禁用主窗口。 这就是我的代码: import time import threading import wx def calculate(): # Simulates the calculation time.sleep(5) return True class CalcFrame(wx.Fra

在我的图形用户界面与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()
但我现在的问题是,如果我按下“计算”按钮,等待框不会显示正确,我看不到文本。我也不能移动/最大化/最小化主窗口。
你能帮助我吗?提前感谢:)

您不应该在主线程以外的线程中更新gui。。。。我很确定wxPython的文档在好几个地方提到了这一点。。它不是线程安全的,而且您的gui处于中断状态

相反,我相信你应该做一些类似的事情

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!)"

谢谢你的帮助,我现在修好了。但我的主要问题仍然存在,等待框没有正确显示(我只能看到文本显示位置的灰色背景-它是白色和深灰色)。哦,好的。。。我会改变答案。。。我就知道你得推迟打电话