How to invoke parent's method?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • many_years_after

    #1

    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.

  • rweth

    #2
    Re: How to invoke parent's method?

    many_years_afte r 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:

    Is applicable.

    Look at the link "Cooperativ e 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(sel f)


    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!


    Comment

    • rweth

      #3
      Re: How to invoke parent's method?

      rweth wrote:
      many_years_afte r 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:

      Is applicable.
      >
      Look at the link "Cooperativ e 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(sel f)
      >
      >
      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(b illy)
      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?














      Comment

      • Frank Millman

        #4
        Re: How to invoke parent's method?


        many_years_afte r 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.Fram e):
        def __init__(self, parent, id, title):
        wx.Frame.__init __(self, parent, id, title)
        MyPanel(self)

        class MyPanel(wx.Pane l):
        def __init__(self,f rame):
        wx.Panel.__init __(self,frame,-1)
        self.frame = frame
        b = wx.Button(self,-1,'Close')
        b.Bind(wx.EVT_B UTTON,self.onCl ose,id=b.GetId( ))

        def onClose(self,ev t):
        self.frame.Clos e()
        evt.Skip()

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

        Comment

        • Peter Decker

          #5
          Re: How to invoke parent's method?

          On 7 Jan 2007 01:33:32 -0800, Frank Millman <frank@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.

          Comment

          Working...