WxPython starts my application twice when importing a subpackage
I apologize for the verbal description.
I have a wxPython application in a file named applicationwindow.py
which is in a package named garlicsimwx
. When I launch the application by running the above file everything works well. However, I created a file rundemo.py
in the folder containing the package garlicsimwx
that also launches the application. When I use rundemo.py
, the application launches, however, when the main one wx.Frame
imports the subpackage garlicsimwx
, namely simulations.life
, for some reason a new instance of my application is created (i.e. a new one will display an identical window.)
I tried to execute the commands one by one, and although the error only occurs after importing the subpackage, the statement import
does not call it directly. Only when control returns to PyApp.MainLoop
does the second window open.
How do you stop this?
a source to share
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 avoid this, use a common Python idiom:
import wx
class MyFrame(wx.Frame):
def __init__(...):
...
if __name__ == '__main__':
frame = MyFrame(...)
Did I understand correctly?
a source to share
You can create a global boolean variable of type g_window_was_drawn
and test it in a function that does the job of creating the window. The value will be false at the start of the program and will change to True when the window is first created. The function that creates the window will check if it is g_window_was_drawn
already true, and if so, this will throw an exception. Then you will have a good spreadsheet that tells you who is responsible for performing this function.
I hope this helps you find it. I apologize for the verbal decision;)
a source to share