How to invoke parent's method?

Frank Millman frank at chagford.com
Sun Jan 7 04:33:32 EST 2007


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




More information about the Python-list mailing list