views:

139

answers:

3

I'm sorry for the verbal description.

I have a wxPython app in a file called applicationwindow.py that resides in a package called garlicsimwx. When I launch the app by launching the aforementioned file, it all works well. However, I have created a file rundemo.py in a folder which contains the garlicsimwx package, which runs the app as well. When I use rundemo.py, the app launches, however, when the main wx.Frame imports a sub-package of garlicsimwx, namely simulations.life, for some reason a new instance of my application is created (i.e., a new identical window pops out.)

I have tried stepping through the commands one-by-one, and although the bug happens only after importing the sub-package, the import statement doesn't directly cause it. Only when control returns to PyApp.MainLoop the second window opens.

How do I stop this?

+3  A: 

I think you have code in one of your modules that looks like this:

import wx

class MyFrame(wx.Frame):
    def __init__(...):
       ...

frame = MyFrame(...)

The frame will be created when this module is first imported. To prevent that, use the common Python idiom:

import wx

class MyFrame(wx.Frame):
    def __init__(...):
       ...

if __name__ == '__main__':
    frame = MyFrame(...)

Did I guess correctly?

Frank Niessink
A fair guess Frank, but no; I have the `if __name__=='__main__'` bit, which calls my `main()` function. In fact, this bug happens even if I comment out these two lines, and call `main()` from `rundemo.py`.
cool-RR
@cool-RR: You may have the if __name__ == "__main__", but you also have some function call that's happening at import time. You have to go through your script and assure that it's ENTIRELY def's and class's and nothing else.
S.Lott
@Frank: I'm sorry, but doesn't Python execute a module only the first time it's imported?
Joril
@Joril: Yes, you are right. Edited my answer.
Frank Niessink
A: 

You could create a global boolean variable like g_window_was_drawn and check it in the function that does the work of creating a window. The value would be false at the start of the program and would change to True when first creating a window. The function that creates the window would check if the g_window_was_drawn is already true, and if it is, it would throw an exception. Then You will have a nice stacktrace telling You who is responsible of executing this function.

I hope that helps You find it. I'm sorry for the verbal solution ;)

Reef
Didn't help - turned out it was a separate process.
cool-RR
But the lack of stacktrace gived You the answer, didn't it? In both times it was executed, g_window_was_drawn was false, so it were separate processes
Reef
Actually yes, it did lead me to that answer. So thanks.
cool-RR
Glad I could help You. I think that as it helped You, it would be fair if You would press the ^ button marked as "This answer is helpful (click again to undo)". You should also mark Your answer as an accepted one, so the question will be displayed in the different color on the main page. If You won't, people will come here and read it, thinking that You still need help (and that would be a waste of time, as You have the answer already).
Reef
A: 

Got it: There was no

if __name__=='__main__':

in my rundemo file. It was actually a multiprocessing issue: The new window was opened in a separate process.

cool-RR