开发者

Get object created in child thread back in main thread

Assume i want to create 500 wxWidget like (some panels , color buttons and text ctrl etc), I have to create all this at single time but this will freeze my main thread, so i put this creation part in child thread and show some gif anim in main thread. But i was not able to get all these wxWidget object those created on my frame in child thread.

Can i get that wxWidgets (created in child thread) back in main thread. simply just consider a case where i have to create children of a frame in child thread and main thread show animation. once child thread finish the all child created in child th开发者_运维问答read should available in main thread.

Any help is really appreciable.

I am using python 2.5, wxpython 2.8 on windowsxp.


You could use pubsub which is included with wxpython -- wx.lib.pubsub.

See my answer here for a basic example of usage for inter-thread comms.


For an alternative: An example of how you could use wx.Yield to keep your window updated.

import wx

class GUI(wx.Frame):
    def __init__(self, parent, title=""):
        wx.Frame.__init__(self, parent=parent, title=title, size=(340,380))
        self.SetMinSize((140,180))

        self.creating_widgets = False

        self.panel = wx.Panel(id=wx.ID_ANY, parent=self)

        self.startButton = wx.Button(self.panel, wx.ID_ANY, 'Start')
        self.stopButton = wx.Button(self.panel, wx.ID_ANY, 'Stop')
        self.messageBox = wx.TextCtrl(self.panel, wx.ID_ANY, '', size=(75, 20))

        self.Bind(wx.EVT_BUTTON,  self.onStart, self.startButton)     
        self.Bind(wx.EVT_BUTTON,  self.onStop, self.stopButton)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.startButton, 0, wx.ALL, 10)
        self.sizer.Add(self.stopButton, 0, wx.ALL, 10)
        self.sizer.Add(self.messageBox, 0, wx.ALL, 10)

        self.panel.SetSizerAndFit(self.sizer)

    def onStart(self, event):
        self.creating_widgets = True
        count = 0
        self.startButton.Disable()
        while self.creating_widgets:
            count += 1
            #Create your widgets here

            #just for simulations sake...
            wx.MilliSleep(100)
            self.messageBox.SetLabel(str(count))

            #Allow the window to update, 
            #You must call wx.yield() frequently to update your window
            wx.Yield()

    def onStop(self, message):
        self.startButton.Enable()
        self.creating_widgets = False


if __name__ == "__main__":

    app = wx.PySimpleApp()
    frame = GUI(None)
    frame.Show()
    app.MainLoop()


You can send them back over a queue, or this all takes place in one instance of a class, assign the widgets to some known place in the instance for the main thread to pick them up. Signal via semaphore.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜