By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,551 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,551 IT Pros & Developers. It's quick & easy.

How to invoke parent's method?

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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.