2013-05-21 58 views
0

我創建了一個簡單的程序,它由一個主框架(Frame)和兩個子框架(ChildFrame1和ChildFrame2)組成。大型機有兩個按鈕,用來檢查ChildFrame1是否已經創建,如果不是則創建相同的,另一個將檢查ChildFrame2是否已經創建,如果不是則創建。現在棘手的部分(至少對我來說很棘手),ChildFrame1有一個按鈕,需要檢查ChildFrame2是否已經從大型機創建,如果不創建它。在我的代碼中,此按鈕只是從主機創建另一個ChildFrame2和以前創建的ChildFrame2。我怎樣才能使這個工作?基本上我有兩個按鈕在兩個框架,但一個事件。wxPython子框架交互

代碼

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() 

回答

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() 
+0

謝謝!有用! – Domagoj