从Python中的不同类调用wx.frame中的函数

时间:2013-10-17 17:22:55

标签: python multithreading wxpython multiprocessing nonblocking

有非常相似的问题,但我要么不理解它们,要么他们没有完全回答它们。我见过的那两个是this onethis one。我有一个运行wxpython的GUI。按下按钮,我运行四个不同的线程(非常长的运行任务)。我已经能够实现这个没问题 - 线程运行,我仍然可以使用GUI。

每个线程(TestThread0TestThread1等)将变量写入文件(但永远不会完成其循环 - 无限循环)。每隔一段时间(比如说每20秒),我想在我的主GUI应用程序(wx.FRAME)中运行一个函数(WriteThis)来读取这个文件及其值/变量。我的问题是如何在线程仍在运行时在GUI部分运行此功能?当我尝试运行TMainForm.WriteThis()时,我的错误发挥作用。

以下是我(非常简短的)代码:

class TMainForm(wx.Frame):

    def __init__(self, *args, **kwds):


            kwds["style"] = wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER
            wx.Frame.__init__(self, *args, **kwds)

            self.Splitter = wx.SplitterWindow(self, -1)#, style=wx.SP_NOSASH)

            self.Panel1 = wx.Panel(self.Splitter, -1)          
            self.Panel3 = wx.Panel(self.Splitter, -1)

            self.Splitter.SplitVertically(self.Panel1,self.Panel3,400)

            ... and so on to set up GUI

    # Press button in GUI to run threads
    def OnAnalyzePress(self,event): 
        TestThread0()
        time.sleep(2)
        TestThread1()
        time.sleep(2)
        TestThread2()
        time.sleep(2)
        TestThread_output

    # This is the function I want to run from TestThread_output class below
    def WriteThis(self):
        print 'Running'
        # I will read file and update GUI here (Threads keep running though)

# Example thread (all the others are the same)
class TestThread0(Thread):

    def __init__(self):
        Thread.__init__(self)
        self.start()    # start the thread

    def run(self):
        # This is my "infinite loop" function that writes variable/values to a file
        MyLongRunningFunction.SomeFunction()

# This is the thread I want to run that executes some function in wx.FRAME every 20 seconds
class TestThread_output(Thread):

    def __init__(self):
        Thread.__init__(self)
        self.start()    # start the thread

    def run(self):

        for i in range(1000):
            TMainForm.WriteThis() # !!! This is where my error is !!! I want to run function called "WriteThis"
            time.sleep(20)

class TApplication(wx.App):
    def OnInit(self):

            wx.InitAllImageHandlers()
            MainForm = TMainForm(None, -1,"")
            self.SetTopWindow(MainForm)

            MainForm.Show()
            return 1

if __name__ == "__main__":
    Application = TApplication(0)
    Application.MainLoop()

感谢您的帮助!!

1 个答案:

答案 0 :(得分:2)

class TestThread_output(Thread):
    def __init__(self,mainForm):
        Thread.__init__(self)
        self.mainForm = mainForm #save reference to the mainFrame GUI
        self.start()    # start the thread
    def run(self):
        for i in range(1000):
            wx.CallAfter(self.mainForm.WriteThis) #since its a diff thread you need callafter(or calllater)
            #I dont think you can do self.mainForm.WriteThis()
            time.sleep(20)

class TMainForm(wx.Frame):
    ...
    def OnAnalyzePress(self,event): 
        TestThread0()
        time.sleep(2)
        TestThread1()
        time.sleep(2)
        TestThread2()
        time.sleep(2)
        TestThread_output(self) #<- pass in this as mainFrame argument to thread constructor