473,473 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to pass globals across modules (wxPython)

My wxPython program starts execution in mainFrame.py like this
[...]
class MainApp(wxApp):
def OnInit(self):
self.mainFrame = MainFrame(None)
self.mainFrame.Show()
self.SetTopWindow(self.mainFrame)
return True
def main():
global application
application=MainApp(0)
application.MainLoop()
if __name__ == '__main__':
main()
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?
Jul 18 '05 #1
9 5335
Martin Drautzburg wrote:
My wxPython program starts execution in mainFrame.py like this
[...]
class MainApp(wxApp):
def OnInit(self):
self.mainFrame = MainFrame(None)
self.mainFrame.Show()
self.SetTopWindow(self.mainFrame)
return True
def main():
global application
application=MainApp(0)
application.MainLoop()
if __name__ == '__main__':
main()
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.
this might help:

http://www.python.org/doc/faq/progra...across-modules
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.


"global" is used in a local scope to tell Python that something isn't a
local variable; see

http://www.python.org/doc/faq/progra...-in-a-function
http://www.python.org/doc/faq/progra...bles-in-python

and

http://docs.python.org/ref/naming.html

for details.

</F>

Jul 18 '05 #2
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
Jul 18 '05 #3
I also struggled with this until I looked into many
of the wxWindows examples. They all tend to pass in
the parent to each subsequent layer of classes so that
they can easily refer backwards in the hierarchy.

Example

In your code you will find that inside of SetTopWindow
you have parent as the first argument. You can refer
to parent.someattribute or parent.somemethod to refer
back to "application", which is instance of MainApp.
Just do something similar in MainFrame class.

Changing MainFrame class a little and passing in
self as the first argument will give you the same
ability inside of MainFrame instance. Something like:

class MainFrame(wxFrame):
def __init__(self, parentclass, parentframe):
self.parentclass=parentclass
wxFrame.__init__(self, parentframe, -1, "Frame Description")
Jul 18 '05 #4
Martin Drautzburg wrote:
My wxPython program starts execution in mainFrame.py like this
[...]
class MainApp(wxApp):
def OnInit(self):
self.mainFrame = MainFrame(None)
self.mainFrame.Show()
self.SetTopWindow(self.mainFrame)
return True
def main():
global application
application=MainApp(0)
application.MainLoop()
if __name__ == '__main__':
main()
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.


If you just need to access the running application from other wxPython
objects, then wx.GetApp() is your friend.

--
Hans Nowak
http://zephyrfalcon.org/

Jul 18 '05 #5
Peter Hansen, Segunda 20 Dezembro 2004 08:01, wrote:
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.


This might be a problem also to share a database connection, where one needs
to pass the open and authenticated connection to several specialized
modules.

Maybe a module where you can access that should be a better option...

Be seeing you,
Godoy.
Jul 18 '05 #6
Jorge Luiz Godoy Filho wrote:
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.


This might be a problem also to share a database connection, where one needs
to pass the open and authenticated connection to several specialized
modules.

Maybe a module where you can access that should be a better option...


or a single "application context class" instance, which is passed to various
parts of the system as necessary.

making subsystems dependent on a module can hinder reuse; making them
dependent on (parts of) the interface of an application context object makes
them a lot easier to reuse.

</F>

Jul 18 '05 #7
Fredrik Lundh, Terça 21 Dezembro 2004 14:02, wrote:
or a single "application context class" instance, which is passed to
various parts of the system as necessary.


Wouldn't that cause a chicken & egg problem? How, then, would one pass such
an instance across modules?

I'm sorry for my ignorance, but I'm a lot more slow than usually today and I
might have missed your point :-)
Be seeing you,
Godoy.
Jul 18 '05 #8
Jorge Luiz Godoy Filho wrote:
or a single "application context class" instance, which is passed to
various parts of the system as necessary.


Wouldn't that cause a chicken & egg problem? How, then, would one pass such
an instance across modules?


well, in my applications, subsystems usually consists of one or more classes, or at least
one or more functions. code that needs the global context usually gets the content either
as a constructor argument, or as an argument to individual methods/functions.

if you build a system by execfile'ing subcomponents (or importing them just to run their
code, rather than use what they define), using this approach is harder. but that's not a
very good way to build systems, so I'm not sure that matters...

</F>

Jul 18 '05 #9
Fredrik Lundh, Terça 21 Dezembro 2004 16:33, wrote:
well, in my applications, subsystems usually consists of one or more
classes, or at least
one or more functions. code that needs the global context usually gets
the content either as a constructor argument, or as an argument to
individual methods/functions.


I see. Differences in terminology. We use the same approach as you do.
Be seeing you,
Godoy.
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

46
by: J.R. | last post by:
Hi folks, The python can only support passing value in function call (right?), I'm wondering how to effectively pass a large parameter, such as a large list or dictionary? It could achieved...
18
by: fred.dixon | last post by:
i have read the book and searched the group too ---------------------------------------------- im not gettin it. i want to read a global (OPTIONS) from file1 from a class method (func1) in file2...
18
by: robert | last post by:
Using global variables in Python often raises chaos. Other languages use a clear prefix for globals. * you forget to declare a global * or you declare a global too much or in conflict * you...
5
by: jgraveskc | last post by:
I know this sounds nasty, but let me explain. I have what was a class that contains a whole buncha function pointers (for OpenGL extensions) that are used in the app. I was porting this app to...
0
by: Astan Chee | last post by:
Hi, I was once using python 2.4 in win2k with wxPython 2.4.2.4 (if im not mistaken) with it. Now i've upgraded to winXP and am using python 2.5 with wxPython 2.8.1.1. The problem Im currently...
5
by: Steven W. Orr | last post by:
I have two seperate modules doing factory stuff which each have the similar function2: In the ds101 module, def DS101CLASS(mname,data): cname = mname+'DS101' msg_class = globals() msg =...
17
by: NoelByron | last post by:
Hello! We are thinking about writing a project for several customers in Python. This project would include (among others) wxPython, a C/C++ module. But what happens if this application generates...
13
by: Bartc | last post by:
I noticed that in C, functions in any module are automatically exported. So that it's not possible to use the same function name in two modules (ie. source files). Now that I know that, I get...
4
by: Ed Bitzer | last post by:
Appreciate some direction as to what most of you professionals do when passing some limited variables to another form. A simple example would be passing the location of a second form. I could use...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.