【问题标题】:Creating child frames of main frame in wxPython在 wxPython 中创建主框架的子框架
【发布时间】:2010-11-14 04:25:03
【问题描述】:

我正在尝试在 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 行),那么 About 框架将成功创建并显示,但在主框架关闭时它保持打开状态。

这是我应该采用的方法来创建主框架的子框架还是应该以不同的方式进行,例如。使用主框架的 onClose 事件来关闭任何子框架(这种方式听起来很“骇人听闻”)。

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

【问题讨论】:

    标签: python wxpython


    【解决方案1】:
    class AboutFrame(wx.Frame):
    
        title = "About this program"
    
        def __init__(self):
            wx.Frame.__init__(self, wx.GetApp().TopWindow, title=self.title)
    

    【讨论】:

    • 我认为不是在 init 中传递 wx.GetApp().TopWindow。应该通过 OnAbout 方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-23
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    相关资源
    最近更新 更多