2

我正在使用 wxpython 做 GUI,我是 python 新手,..我做了一个 GUI 说 Mainframe,当我点击它时它有一个按钮弹出一个新框架说子框架。

我想知道如何在子框架打开时隐藏大型机以及如何从子框架返回大型机。

希望得到好的建议

提前致谢

4

2 回答 2

4

我使用 Pubsub 来做这种事情。我实际上在这里写了一个关于这个过程的教程:http: //www.blog.pythonlibrary.org/2010/06/27/wxpython-and-pubsub-a-simple-tutorial/

如果您想从子框架终止程序,那么您需要向父框架发送一条消息,告诉其关闭/销毁自身。您可以尝试将父框架的引用传递给子框架并关闭它,但我怀疑这会导致错误,因为它会在子框架之前破坏父框架。

于 2013-01-02T15:47:54.050 回答
2

使用隐藏和显示方法。
在此示例中,父框架和子框架在按下按钮时相互替换:

import wx

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)
        self.button = wx.Button(self, wx.ID_ANY, "Parent")
        self.child = None

        self.Bind(wx.EVT_BUTTON, self.onbutton, self.button)
        self.SetTitle("myframe")

    def onbutton(self, evt):
        if not self.child:             # if the child frame has not been created yet, 
            self.child = Child(self)   # create it, making it a child of this one (self)
        self.child.Show()              # show the child
        self.Hide()                    # hide this one


class Child(wx.Frame):
    def __init__(self, parent, *args, **kwds):            # note parent outside *args               
        wx.Frame.__init__(self, parent, *args, **kwds)
        self.button = wx.Button(self, wx.ID_ANY, "Child")
        self.parent = parent                              # this is my reference to the
                                                          # hidden parent 
        self.Bind(wx.EVT_BUTTON, self.onbutton, self.button)
        self.SetTitle("child")

    def onbutton(self, evt):
        self.parent.Show()               # show the parent
        self.Hide()                      # hide this one


if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    frame = MyFrame(None, wx.ID_ANY, "")
    app.SetTopWindow(frame)
    frame.Show()
    app.MainLoop()
于 2012-12-31T10:47:19.330 回答