Problem with several calls of custom main loop

Hi,

I am having problems running my wxPython app with a custom main loop. Specifically, the problem arises when starting my application a second time from with the same python process. I discovered this because of trouble with pytest. Here is a minimal example:

···

##################################

import time

import wx

def run():

app = wx.App()

frame = wx.Frame(None)

frame.Show()

custom_mainloop(frame)

def custom_mainloop(mainframe):

oldloop = wx.EventLoop.GetActive()

evtloop = wx.GUIEventLoop()

wx.EventLoop.SetActive(evtloop)

while isinstance(mainframe, wx.Frame):

	while evtloop.Pending():

		evtloop.Dispatch()

	evtloop.ProcessIdle()

	time.sleep(0.1)

	print type(mainframe)

wx.EventLoop.SetActive(oldloop)

if name == “main”:

print "run 1"

run()

print "run 2"

run()

print "run 3"

run()

###############################

During run 1, everything is fine. The loop detects when the user closes the frame, exits and the second run is started. This is because when the frame gets closed, it is destroyed and the type of the handler changes to wx._core._wxPyDeadObject. In the second run, however, even after having closed the frame, the type statement still returns wx._windows.Frame. As a consequence, the loop is never left and run 3 is never started. If I replace the call of custom_mainloop(frame) by app.mainloop(), all three runs are executed as expected. Thus, I guess the problem is that I am missing some deletion or cleaning after having left the main loop.

Any help would be very appreciated!

Thank you in advance

Best

Timo

I just read the info concerning code samples. Please find the minimal example also attached…

minimal.py (528 Bytes)

···

Am Samstag, 16. März 2013 11:51:01 UTC+1 schrieb Suboptimist:

Hi,

I am having problems running my wxPython app with a custom main loop. Specifically, the problem arises when starting my application a second time from with the same python process. I discovered this because of trouble with pytest. Here is a minimal example:

##################################

import time

import wx

def run():

app = wx.App()

frame = wx.Frame(None)

frame.Show()

custom_mainloop(frame)

def custom_mainloop(mainframe):

oldloop = wx.EventLoop.GetActive()

evtloop = wx.GUIEventLoop()

wx.EventLoop.SetActive(evtloop)

while isinstance(mainframe, wx.Frame):

  while evtloop.Pending():
  	evtloop.Dispatch()
  evtloop.ProcessIdle()
  time.sleep(0.1)
  print type(mainframe)

wx.EventLoop.SetActive(oldloop)

if name == “main”:

print “run 1”

run()

print “run 2”

run()

print “run 3”

run()

###############################

During run 1, everything is fine. The loop detects when the user closes the frame, exits and the second run is started. This is because when the frame gets closed, it is destroyed and the type of the handler changes to wx._core._wxPyDeadObject. In the second run, however, even after having closed the frame, the type statement still returns wx._windows.Frame. As a consequence, the loop is never left and run 3 is never started. If I replace the call of custom_mainloop(frame) by app.mainloop(), all three runs are executed as expected. Thus, I guess the problem is that I am missing some deletion or cleaning after having left the main loop.

Any help would be very appreciated!

Thank you in advance

Best

Timo