Re: how to pass globals across modules (wxPython)

2004-12-22 Thread Jorge Luiz Godoy Filho
Fredrik Lundh, Tera 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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to pass globals across modules (wxPython)

2004-12-21 Thread Jorge Luiz Godoy Filho
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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to pass globals across modules (wxPython)

2004-12-21 Thread Fredrik Lundh
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 



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to pass globals across modules (wxPython)

2004-12-21 Thread Fredrik Lundh
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 



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to pass globals across modules (wxPython)

2004-12-20 Thread Larry Bates
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)
.
.
.
Then in Main App pass self as first argument (parentclass),
then you can refer back to MainApp instance as
self.parentclass.  If you go several levels down you
get self.parentclass.parentclass.parentclass.attribute:
class MainApp(wxApp):
def OnInit(self):
self.mainFrame = MainFrame(self, None)
self.mainFrame.Show()
self.SetTopWindow(self.mainFrame)
return True
This might not be the best way, but seems to work and models
what wxWindows appears to do internally.
Larry Bates
Syscon, Inc.

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.
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?
--
http://mail.python.org/mailman/listinfo/python-list


Re: how to pass globals across modules (wxPython)

2004-12-20 Thread Hans Nowak
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/
--
http://mail.python.org/mailman/listinfo/python-list