How to invoke parent's method?

Frank Millman frank at
Sun Jan 7 10:33:32 CET 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)

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

More information about the Python-list mailing list