473,657 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
+ 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.SetTopWind ow(self.mainFra me)
return True
def main():
global application
application=Mai nApp(0)
application.Mai nLoop()
if __name__ == '__main__':
main()
I need to access the "applicatio n" 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 5348
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.SetTopWind ow(self.mainFra me)
return True
def main():
global application
application=Mai nApp(0)
application.Mai nLoop()
if __name__ == '__main__':
main()
I need to access the "applicatio n" 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=Mai nApp(0)
application.Mai nLoop()

I need to access the "applicatio n" 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 "applicatio n globals" like
this, say "globals.py ". Then you can import that in your
main script, and do "globals.applic ation = 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.someattr ibute or parent.somemeth od to refer
back to "applicatio n", 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(wxFra me):
def __init__(self, parentclass, parentframe):
self.parentclas s=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.SetTopWind ow(self.mainFra me)
return True
def main():
global application
application=Mai nApp(0)
application.Mai nLoop()
if __name__ == '__main__':
main()
I need to access the "applicatio n" 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 "applicatio n 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 "applicatio n 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 "applicatio n 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
3502
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 by pointer in C++, is there such way in Python? Thansk in advance. J.R.
18
1385
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 i want to understand how this works. ---------------------------------------------- #file1.py
18
2939
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 have a local identical variable name and want to save/load it to/from the global with same name * while you add code, the definition of globals moves more and more apart from their use cases -> weirdness; programmers thinking is fragmented * using...
5
1852
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 macs, where the extensions are properly present (just normal functions) so the class is not needed. I was wanting a way to make the same code work on both. For example, if I could do this: glGenBuffersARB(bla); on both. On the mac its just...
0
857
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 having is that in my code: from wxPython.wx import * from wxPython.grid import * from wxPython.html import *
5
1289
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 = msg_class(data) return msg
17
1535
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 a segmentation fault on a customers PC. What changes do we have to trace back the source of the error? Imagine you use two or three C libraries in a Python project and you experience random crashes in 'Python.exe'. What would you do?
13
1154
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 work around it; but is there a way to avoid the problem (short of lots of renaming)? More seriously, variables declared at file scope also seem to be automatically exported. But in this case, the compiler/linker doesn't warn me that the same name...
4
1143
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 a constructor which I think of as parameters attached to the creation of my New form (parameters) or I could use "properties" as I have learned to use with classes. Some advice please. Ed
0
8407
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8319
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8612
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7347
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2739
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 we have to send another system
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.