How to invoke parent's method?

Discussion in 'Python' started by many_years_after, Jan 7, 2007.

  1. 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
    many_years_after, Jan 7, 2007
    1. Advertisements

  2. many_years_after

    rweth Guest

    I think it "could" if what I read recently in:
    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):

    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!
    rweth, Jan 7, 2007
    1. Advertisements

  3. many_years_after

    rweth Guest

    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:
    .... def methodx(self):
    .... print "DAD-methodx"
    ........ def methodx(self):
    .... print "SON-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?
    rweth, Jan 7, 2007
  4. Have a look at the following program -


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

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

    def onClose(self,evt):

    class MyApp(wx.App):
    def OnInit(self):
    frame = MyFrame(None, -1, "Test")
    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.


    Frank Millman
    Frank Millman, Jan 7, 2007
  5. many_years_after

    Peter Decker Guest

    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'.
    Peter Decker, Jan 7, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.