Hi,
the following code relies on wx.Frame.GetChildren() to return the list of
all its children. I use this to wait for the event that a child object is
attached to the parent frame. The attachment of the object occurs in the
main wx.App thread and I wait for it from another thread. This worked fine
until at least the latest 2.8 release but with the latest 2.9 release and
any version later it does not work anymore. GetChildren() always returns an
empty list. Am I doing something wrong or was there an intended change of
anything related to my problem?
Regards, Christian
import wx
import wx.lib.newevent
from threading import Thread
import time
import pythoncom
(RunEvent, EVT_RUN) = wx.lib.newevent.NewEvent()
class App(wx.App):
def init(self):
wx.App.init(self, False)
self.result = None
def OnInit(self):
self.evttarget = self.f = wx.Frame(None)
self.f.Bind(wx.EVT_CLOSE, self.OnClose)
self.f.Bind(EVT_RUN, self.OnRun)
return True
def OnClose(self, evt):
self.f.Show(False)
self.f.DestroyChildren()
def OnRun(self, evt):
callable = getattr(self, evt.callable)
if hasattr(evt, 'arg'):
self.result = callable(self.f, *evt.arg)
else:
self.result = callable(self.f)
def End(self, parent):
self.f.Destroy()
def hello_world(self, parent):
dlg = wx.MessageDialog(parent, 'hello world', style=wx.OK)
dlg.Raise()
if dlg.ShowModal() == wx.ID_OK:
print 'ok'
dlg.Destroy()
return sbj
class AppLauncher(Thread):
def init(self):
Thread.init(self)
def start(self):
if not hasattr(self, 'app'):
Thread.start(self)
time.sleep(1)
def launch(self, callable, *arg, **kwargs):
print 'launching',callable,
evt = RunEvent(callable=callable, arg=arg)
wx.PostEvent(self.app.evttarget, evt)
print 'wait to show'
# wait until shown
while len(self.app.evttarget.GetChildren()) == 0:
pythoncom.PumpWaitingMessages()
time.sleep(0.3)
print 'wait to hide'
# wait until hidden
while len(self.app.evttarget.GetChildren()) > 0:
pythoncom.PumpWaitingMessages()
time.sleep(0.3)
return self.app.result
def run(self):
print 'applauncher started'
self.app = App()
self.app.MainLoop()
print 'applauncher terminated'
def quit(self):
evt = RunEvent(callable='End')
wx.PostEvent(AppLauncher.app.evttarget, evt)
def __call__(self):
return self
AppLauncher = AppLauncher()
if name == ‘main’:
AppLauncher.start()
AppLauncher.launch(‘hello world’)
AppLauncher.quit()