0

我创建了一个简单的程序,它由一个大型机(框架)和两个子框架(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()
4

1 回答 1

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()
于 2013-05-21T12:14:05.570 回答