在wxPython中创建主框架的子框架 [英] Creating child frames of main frame in wxPython

查看:32
本文介绍了在wxPython中创建主框架的子框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 wxPython 中创建一个新框架,它是主框架的子框架,以便当主框架关闭时,子框架也将关闭.

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()

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

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.

这是我应该采用的方法来创建主框架的子框架还是应该以不同的方式来做,例如.使用主框架的 onClose 事件关闭所有子框架(这种方式听起来很hackish").

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?

推荐答案

class AboutFrame(wx.Frame):

    title = "About this program"

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

这篇关于在wxPython中创建主框架的子框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆