Sanne Korzec wrote:
Hi mailing,
I'm writing a Wxpython program with the following classes:
A wxpython GUI class (or app if you will) which handles all my events and a
separate class with some methods...GUI:
-update_picture_in_this_panel
_init_class-A
etc.Class A:
-do_some_stuff
-when_doing_stuff.
Etc.At some point in the GUI, a handler initializes class A. However, I would
also like to call the GUI.update_picture_in_this_panel method from class A.Under normal circumstances, I would simply rearrange the methods or make
some global method which can be called everywhere, but since the GUI needs a
lot of different objects, this will be a very messy solution. I was hoping
somebody knows a workaround...
This is very similar to another discussion we've been having on this list!
There are several ways to do this. One common way is for GUI to pass its "self" object to the A constructor it creates an instance of A. Remember that A does not need to import the GUI module in order to call methods on a GUI instance. As long as you have an instance, you can call methods. So:
In file GUI:
import A
class GUI:
def init_class_a( self ):
self.a_object = A.A( self )
...
In file A:
class A:
def __init__( self, guiobj ):
self.gui = guiobj
...
def when_doing_stuff( self ):
self.gui.update_picture_in_this_panel()
If you don't want to have the entire GUI object available, you can also just pass the the one function, kind of like a callback:
In file GUI:
import A
class GUI:
def init_class_a( self ):
# Note no () here; we want to PASS the function, not CALL the function.
self.a_object = A.A( self.update_picture_in_this_panel )
...
In file A:
class A:
def __init__( self, guiupdate ):
self.guifunction = guiupdate
...
def when_doing_stuff( self ):
self.guifunction()
ยทยทยท
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.