【问题标题】:wxPython childframes interactionwxPython 子框架交互
【发布时间】:2013-05-21 09:53:44
【问题描述】:

我创建了一个简单的程序,它由一个大型机(框架)和两个子框架(ChildFrame1 和 ChildFrame2)组成。 Mainframe 有两个按钮,一个将检查 ChildFrame1 是否已创建,如果没有则创建相同的按钮,另一个将检查 ChildFrame2 是否已创建,如果没有则创建相同的按钮。现在棘手的部分(至少对我来说很棘手),ChildFrame1 有一个按钮,需要检查 ChildFrame2 是否已经从大型机创建,如果没有创建它。在我的代码中,此按钮仅在先前从大型机创建的 ChildFrame2 旁边创建另一个 ChildFrame2。我怎样才能使它起作用?基本上我在两个框架上有两个按钮,但只有一个事件。

代码

import wx

class Frame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY,'Parent')
        panel = wx.Panel(self, -1)

        sizer = wx.BoxSizer(wx.VERTICAL)
        button = wx.Button(panel, -1, 'Open Child1')
        button2 = wx.Button(panel, -1, 'Open Child2')

        sizer.Add(button, 0, wx.CENTER|wx.ALL, 5)
        sizer.Add(button2, 0, wx.CENTER|wx.ALL, 5)

        panel.SetSizer(sizer)

        self.Bind(wx.EVT_BUTTON, self.onButton,  button)
        self.Bind(wx.EVT_BUTTON, self.onButton2,  button2)

    def onButton(self, e):
        try:
            self.ChildF.Show()
        except:
            self.ChildF = ChildFrame1()
            self.ChildF.Show()

        self.ChildF.SetFocus()

    def onButton2(self, e):
        try:
            self.ChildF2.Show()
        except:
            self.ChildF2 = ChildFrame2()
            self.ChildF2.Show()

        self.ChildF2.SetFocus()

class ChildFrame1(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, wx.GetApp().TopWindow, wx.ID_ANY, 'Child1')
        panel = wx.Panel(self, -1)

        sizer = wx.BoxSizer(wx.VERTICAL)
        button = wx.Button(panel, -1, 'Open Child2')
        sizer.Add(button, 0, wx.CENTER|wx.ALL, 5)

        panel.SetSizer(sizer)

        self.Bind(wx.EVT_BUTTON, self.OnButton, button)

    def OnButton(self,e):
        try:
            self.ChildF.Show()
        except:
            self.ChildF = ChildFrame2()
            self.ChildF.Show()

        self.ChildF.SetFocus()

class ChildFrame2(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, wx.GetApp().TopWindow, wx.ID_ANY, 'Child2')

if __name__ == '__main__':
    app = wx.App()
    frame = Frame().Show()
    app.MainLoop()

【问题讨论】:

    标签: python wxpython parent-child


    【解决方案1】:

    您可以将 ChildFrame1 的按钮处理程序绑定到父框架方法以打开/显示 ChildFrame2。

    import wx
    
    
    class Frame(wx.Frame):
        def __init__(self):
            wx.Frame.__init__(self, None, wx.ID_ANY, 'Parent')
            panel = wx.Panel(self, -1)
    
            sizer = wx.BoxSizer(wx.VERTICAL)
            button = wx.Button(panel, -1, 'Open Child1')
            button2 = wx.Button(panel, -1, 'Open Child2')
    
            sizer.Add(button, 0, wx.CENTER | wx.ALL, 5)
            sizer.Add(button2, 0, wx.CENTER | wx.ALL, 5)
    
            panel.SetSizer(sizer)
    
            self.Bind(wx.EVT_BUTTON, self.onButton, button)
            self.Bind(wx.EVT_BUTTON, self.onButton2, button2)
    
        def onButton(self, e):
            try:
                self.ChildF.Show()
            except:
                self.ChildF = ChildFrame1()
                self.ChildF.Show()
    
            self.ChildF.SetFocus()
    
        def onButton2(self, e):
            try:
                self.ChildF2.Show()
            except:
                self.ChildF2 = ChildFrame2()
                self.ChildF2.Show()
    
            self.ChildF2.SetFocus()
    
    
    class ChildFrame1(wx.Frame):
        def __init__(self):
            wx.Frame.__init__(self, wx.GetApp().TopWindow, wx.ID_ANY, 'Child1')
            panel = wx.Panel(self, -1)
    
            sizer = wx.BoxSizer(wx.VERTICAL)
            button = wx.Button(panel, -1, 'Open Child2')
            sizer.Add(button, 0, wx.CENTER | wx.ALL, 5)
    
            panel.SetSizer(sizer)
    
            self.Bind(wx.EVT_BUTTON, self.GetParent().onButton2, button)
    
    
    class ChildFrame2(wx.Frame):
        def __init__(self):
            wx.Frame.__init__(self, wx.GetApp().TopWindow, wx.ID_ANY, 'Child2')
    
    if __name__ == '__main__':
        app = wx.App()
        frame = Frame().Show()
        app.MainLoop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-14
      • 1970-01-01
      • 2016-10-29
      • 1970-01-01
      • 2010-12-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多