469,312 Members | 2,524 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,312 developers. It's quick & easy.

How to invoke parent's method?

Hi, pythoners:

My wxPython program includes a panel whose parent is a frame. The
panel has a button. When I click the button , I want to let the frame
destroy. How to implement it? Could the panel invoke the frame's
method?
Thanks.

Jan 7 '07 #1
4 5896
many_years_after wrote:
Hi, pythoners:

My wxPython program includes a panel whose parent is a frame. The
panel has a button. When I click the button , I want to let the frame
destroy. How to implement it? Could the panel invoke the frame's
method?
Thanks.
I think it "could" if what I read recently in:
http://www.python.org/download/relea...o/#cooperation
Is applicable.

Look at the link "Cooperative methods and super"
Write a subclass of panel where the target method of it's parent
is targX (that parent method of Frame that you want to call)

class myPanel (Panel):
def dadsX (self):
Frame.targX(self)
I think you are forced to invoke this within a method of the Class
itself, as opposed to doing so with an instance.

Good luck!
Jan 7 '07 #2
rweth wrote:
many_years_after wrote:
>Hi, pythoners:

My wxPython program includes a panel whose parent is a frame. The
panel has a button. When I click the button , I want to let the frame
destroy. How to implement it? Could the panel invoke the frame's
method?
Thanks.
I think it "could" if what I read recently in:
http://www.python.org/download/relea...o/#cooperation
Is applicable.

Look at the link "Cooperative methods and super"
Write a subclass of panel where the target method of it's parent
is targX (that parent method of Frame that you want to call)

class myPanel (Panel):
def dadsX (self):
Frame.targX(self)
I think you are forced to invoke this within a method of the Class
itself, as opposed to doing so with an instance.

Good luck!

You know I just tried a code fragment and got the parent method to work
with an instance .. so maybe you don't have to subclass. Here is a
transcript illustrating the idea:
>>class DAD:
.... def methodx(self):
.... print "DAD-methodx"
....
>>class SON(DAD):
.... def methodx(self):
.... print "SON-methodx"
....
>>>

billy = SON()
billy.methodx()
SON-methodx
>>DAD.methodx(billy)
DAD-methodx
>>>
So you can invoke the DAD.methodx via the billy instance of the object
... without subclassing. Now I wonder if this will actually work?




Jan 7 '07 #3

many_years_after wrote:
Hi, pythoners:

My wxPython program includes a panel whose parent is a frame. The
panel has a button. When I click the button , I want to let the frame
destroy. How to implement it? Could the panel invoke the frame's
method?
Thanks.
Have a look at the following program -

----------------------------------------------------------------------

class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title)
MyPanel(self)

class MyPanel(wx.Panel):
def __init__(self,frame):
wx.Panel.__init__(self,frame,-1)
self.frame = frame
b = wx.Button(self,-1,'Close')
b.Bind(wx.EVT_BUTTON,self.onClose,id=b.GetId())

def onClose(self,evt):
self.frame.Close()
evt.Skip()

class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, "Test")
frame.Show(True)
self.SetTopWindow(frame)
return True

app = MyApp(0) # Create an instance of the application class
app.MainLoop() # Tell it to start processing events

----------------------------------------------------------------------

The essential point is that you save a reference to the frame when you
create the panel. Then when the button is clicked you can use the
reference to call the frame's Close method.

HTH

Frank Millman

Jan 7 '07 #4
On 7 Jan 2007 01:33:32 -0800, Frank Millman <fr***@chagford.comwrote:
The essential point is that you save a reference to the frame when you
create the panel. Then when the button is clicked you can use the
reference to call the frame's Close method.
Or you could look into Dabo. This is one of those common needs that
has been built into the framework. Any object on a form (a wx.Frame)
can simple reference 'self.Form' to get a reference to the containing
frame. Controls that are contained within other controls (such as
inside panels or notebook pages) can always reference that container
with 'self.Parent'.

--

# p.d.
Jan 7 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by FeOl | last post: by
1 post views Thread by Mel | last post: by
1 post views Thread by tolisss | last post: by
1 post views Thread by - vhannak | last post: by
1 post views Thread by Danny Ni | last post: by
5 posts views Thread by puzzlecracker | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.