在wxPython中创建主框架的子框架

时间:2009-07-26 18:08:57

标签: python wxpython

我正在尝试在wxPython中创建一个新帧,它是主帧的子帧,这样当主帧关闭时,子帧也将被关闭。

以下是我遇到的问题的简化示例:

#! /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()

如果我将AboutFrame的父框架设置为None(在第48行),则会成功创建并显示“关于”框架,但在主框架关闭时它将保持打开状态。

这是我应该采取的方法来创建主框架的子框架,还是我应该采用不同的方式,例如。使用主框架的onClose事件来关闭所有子框架(这种方式听起来非常'hackish')。

如果我采取正确的方法,为什么它不起作用?

1 个答案:

答案 0 :(得分:10)

class AboutFrame(wx.Frame):

    title = "About this program"

    def __init__(self):
        wx.Frame.__init__(self, wx.GetApp().TopWindow, title=self.title)