我创建了一个简单的程序,它由一个主机(Frame)和两个子框架(ChildFrame1和ChildFrame2)组成。大型机有两个按钮,一个将检查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()
答案 0 :(得分: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()