how to pass globals across modules (wxPython)
Peter Hansen
peter at engcorp.com
Mon Dec 20 05:01:05 EST 2004
More information about the Python-list mailing list
Mon Dec 20 05:01:05 EST 2004
- Previous message (by thread): how to pass globals across modules (wxPython)
- Next message (by thread): how to pass globals across modules (wxPython)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Martin Drautzburg wrote: > My wxPython program starts execution in mainFrame.py like this > def main(): > global application > application=MainApp(0) > application.MainLoop() > > I need to access the "application" object from other modules, actually > the windows and frames that live therein and I don't know how to do > this. > > I tried using "global", but that does not seem to help. In the other > module I run an "import mainFrame" and that seems to reset the global > variables. > > Am I missing something obvious? Not exactly obvious, perhaps, but well-defined. When you run a Python script, the first script executed (the one specified on the command line) is given the name __main__ in the list of loaded modules (sys.modules, specifically). If you do an "import mainFrame", you are actually loading a second copy of the module into memory, under a different name. The solution to your specific problem, without changing any of the structure of your program, is to do "import __main__" instead, but that's ultimately not likely the best solution. One slightly better solution would be to create a special empty module, just for holding "application globals" like this, say "globals.py". Then you can import that in your main script, and do "globals.application = MainApp(0)", then import the same name elsewhere and work with it as you are trying to. An even better approach might be to find a way to avoid having to access the main window through a global, but I'll have to leave this up to you, as it may depend on your program structure. -Peter
- Previous message (by thread): how to pass globals across modules (wxPython)
- Next message (by thread): how to pass globals across modules (wxPython)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list