12

I am trying create a new frame in wxPython that is a child of the main frame so that when the main frame is closed, the child frame will also be closed.

Here is a simplified example of the problem that I am having:

#! /usr/bin/env python

import wx

class App(wx.App):

    def OnInit(self):
       frame = MainFrame()
       frame.Show()
       self.SetTopWindow(frame)
       return True

class MainFrame(wx.Frame):

    title = "Main Frame"

    def __init__(self):
        wx.Frame.__init__(self, None, 1, self.title) #id = 5

        menuFile = wx.Menu()

        menuAbout = wx.Menu()
        menuAbout.Append(2, "&About...", "About this program")

        menuBar = wx.MenuBar()
        menuBar.Append(menuAbout, "&Help")
        self.SetMenuBar(menuBar)

        self.CreateStatusBar()

        self.Bind(wx.EVT_MENU, self.OnAbout, id=2)

    def OnQuit(self, event):
        self.Close()

    def OnAbout(self, event):
        AboutFrame().Show()

class AboutFrame(wx.Frame):

    title = "About this program"

    def __init__(self):
        wx.Frame.__init__(self, 1, -1, self.title) #trying to set parent=1 (id of MainFrame())


if __name__ == '__main__':
    app = App(False)
    app.MainLoop()

If I set AboutFrame's parent frame to None (on line 48) then the About frame is succesfully created and displayed but it stays open when the main frame is closed.

Is this the approach that I should be taking to create child frames of the main frame or should I be doing it differently, eg. using the onClose event of the main frame to close any child frames (this way sounds very 'hackish').

If I am taking the correct approach, why is it not working?

1 Answer 1

10
class AboutFrame(wx.Frame):

    title = "About this program"

    def __init__(self):
        wx.Frame.__init__(self, wx.GetApp().TopWindow, title=self.title)
Sign up to request clarification or add additional context in comments.

1 Comment

I think instead of passing wx.GetApp().TopWindow inside init. It should be passed OnAbout method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.