Application and MainWindow: recursive imports

24 views
Skip to first unread message

moo...@posteo.org

unread,
Feb 22, 2015, 1:04:37 PM2/22/15
to wxpytho...@googlegroups.com
I want to know how to organize the files holding the my wx.App-derived
application class and my wx.Frame-derived MainWindow-GUI class.

My app-class does create an instance of the main window in its OnInit()
and manage the loading and storing of the config-file-object.
And because it is the application it hold informations about the
version and things like that.

So the MainWindow ask the app object for the version string to display
it. It is just an example of a lot of other cases. So please don't
focus on the version string. :)

That is why the MainWindow need to know the App-Object. And the App
need to know the MainWindow to create an instance of it and call Show().
That causes recursive import problems.

And currently I ran into a horrible misstake about it. The file with
the App in it is imported too times which is no problem. But global
object instances (for example the Config-object) are overridden because
of that.

So I see I have to reorganize the current situation. But how?

Where do you "store" your App and MainWindow class?
How do they know each other?

Are you interested in my recursive import. This is pseudo-code
[APP-file]
from GUI-file import MainWindow

class Config:
pass

c = Config()

class App:
def OnInit(self):
self.LoadConfigFromFile()
MainWindow().Show()
return True

def OnExit(self):
self.StoreConfigToFile()
return super(App, self).OnExit()

if __name__ == '__main__':
App().MainLoop()
[/APP-file]

[GUI-file]
# NO import of the APP-file here!
class MainWindow:
def DoSomething(self):
# access the module-global-config object
App.config.DoSomething()

# HERE come the import
import App
#...
[/GUI-file]

I am not sure why it work even. :D

Michael Ross

unread,
Feb 22, 2015, 2:00:15 PM2/22/15
to wxpytho...@googlegroups.com, moo...@posteo.org
On Sun, 22 Feb 2015 19:03:37 +0100, <moo...@posteo.org> wrote:

> I want to know how to organize the files holding the my wx.App-derived
> application class and my wx.Frame-derived MainWindow-GUI class.
>
> My app-class does create an instance of the main window in its OnInit()
> and manage the loading and storing of the config-file-object.
> And because it is the application it hold informations about the
> version and things like that.
>
> So the MainWindow ask the app object for the version string to display
> it. It is just an example of a lot of other cases. So please don't
> focus on the version string. :)
>
> That is why the MainWindow need to know the App-Object. And the App
> need to know the MainWindow to create an instance of it and call Show().
> That causes recursive import problems.
>
> And currently I ran into a horrible misstake about it. The file with
> the App in it is imported too times which is no problem. But global
> object instances (for example the Config-object) are overridden because
> of that.
>
> So I see I have to reorganize the current situation. But how?

Try this:

Put your config into an extra module, like so:
[configmodule]

class _config(object):
def __init__(self):
pass

config=_config()


and then import in your other modules:

from configmodule import config


Works for me. Void where prohibited ...

Michael

Dev Player

unread,
Feb 22, 2015, 7:46:34 PM2/22/15
to wxpython-users
[app.py]
import wx
from . import mainframe
from . import config # make sure you are loading local config.py and not one in site-packages

class App(wx.App):
   def OnInit(self):
      self.config = config.Config()
      self.MainFrame = mainframe.MainFrame()
[\app.py]

[mainframe.py]
import wx
from . import config

class MainFrame(wx.Frame):
    def DoSomething(self):
        # access the module-global-config object
        wx.GetApp().config.DoSomething()

       # or more robustly
       app = wx.GetApp()
       if app and hasattr(app, 'config.DoSomething') and callable(app.config.DoSomething):
           result = app.config.DoSomething()
[\mainframe.py]





--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Charles J. Daniels

unread,
Feb 25, 2015, 2:56:46 AM2/25/15
to wxpytho...@googlegroups.com, moo...@posteo.org


I for one don't use an App class. I just create an app instance in my main function. Then you instantiate your main window and show it, start your app main loop -- but App doesn't need to know about the top level window.

And any code that needs to access the app, I use wx.GetApp() 
Reply all
Reply to author
Forward
0 new messages