[wxPython] Accessing variables from a child-class

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[wxPython] Accessing variables from a child-class

Jochen "SchXfer"
Hello

I'm a wxPython-newbie and have problems to access the variables of a
parrent-class from its child-class.
I need to make a child-window in which I select some Files and set some
variables to the chosen file-path in the main-Window-class after I push
a button.
But I can only access the parrent-class from the __init__(self, parent,
...) - method.
How can I access the parrent-class from a method OnClick(self,event)
which is triggered by an event EVT_BUTTON(self, 1, self.OnClick)
Could anyone help to solve this problem?
Thanks.

Jochen Schaefer

_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com



_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users



Reply | Threaded
Open this post in threaded view
|

Re: [wxPython] Accessing variables from a child-class

Sam Pumphrey
"Jochen Schäfer" wrote:

>
> Hello
>
> I'm a wxPython-newbie and have problems to access the variables of a
> parrent-class from its child-class.
> I need to make a child-window in which I select some Files and set some
> variables to the chosen file-path in the main-Window-class after I push
> a button.
> But I can only access the parrent-class from the __init__(self, parent,
> ...) - method.
> How can I access the parrent-class from a method OnClick(self,event)
> which is triggered by an event EVT_BUTTON(self, 1, self.OnClick)
> Could anyone help to solve this problem?

In the __init__ method, include the following line:

self.parent_ = parent

and in OnClick, access the parent through 'self.parent_'.

---- Technical note:

The only problem then is that you now have a circular reference: parent
has a reference to child and child has a reference to parent. This can
be a problem in certain cases as Python uses a reference counting system
to do its garbage collection, and so it is good practice to make a
further method, perhaps called RemoveCircularReferences, which does the
following:

  def RemoveCircularReferences(self):
    del self.parent_

and this should be called from the parent as:

  self.child.RemoveCircularReferences()
  del self.child

in order to cleanly get rid of the child and parent.

----

Sam

------------------------------------------------------
   Sam Pumphrey - Project SPIRIT, AT&T Research UK
 Email: [hidden email] - Tel: (01223) 343215
------------------------------------------------------

_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users