2015-05-05 52 views
0

我有一個框架包含wx.SplitterWindow與兩個面板作爲其子。我想用一個按鈕(顯示/隱藏按鈕)隱藏其中一個面板,即第一個面板應該隱藏,而第二個面板應該佔用整個框架,並且框格消失。再次單擊該按鈕應顯示隱藏的面板,並將窗框放回原位。這可能嗎?wxpython:隱藏一個splitterWindow面板

我搜查了文檔,似乎沒有具體的方法來做到這一點?這怎麼能實現。

import wx 
class MainFrame(wx.Frame): 
    """""" 

    #---------------------------------------------------------------------- 
    def __init__(self): 
     wx.Frame.__init__(self, None, title="test", size=(800,600)) 

     self.splitter = wx.SplitterWindow(self, wx.ID_ANY) 
     self.panelOne = wx.Panel(self.splitter,1) 
     self.panelTwo = wx.Panel(self.splitter,1) 
     self.panelOne.SetBackgroundColour('sky blue') 
     self.panelTwo.SetBackgroundColour('pink') 
     self.splitter.SplitHorizontally(self.panelOne, self.panelTwo) 
     self.splitter.SetMinimumPaneSize(20) 

     self.buttonpanel = wx.Panel(self, 1) 
     self.buttonpanel.SetBackgroundColour('white') 
     self.mybutton = wx.Button(self.buttonpanel,label = "Hide") 
     self.Bind(wx.EVT_BUTTON, self.show_hide, self.mybutton) 

     self.sizer = wx.BoxSizer(wx.VERTICAL) 
     self.sizer.Add(self.splitter, 2, wx.EXPAND) 
     self.sizer.Add(self.buttonpanel, 0, wx.EXPAND) 
     self.SetSizer(self.sizer) 

    def show_hide(self, event): 
     label = self.mybutton.GetLabel() 
     if label == "Hide": 
      ### How to Hide panelOne ?? 
      self.mybutton.SetLabel("Show") 
     if label == "Show": 
      ### How to Show panelOne ?? 
      self.mybutton.SetLabel("Hide") 


if __name__ == "__main__": 
    app = wx.App(False) 
    frame = MainFrame() 
    frame.Show() 
    app.MainLoop() 

回答

1

閱讀documentation幾秒鐘後,我注意到了Unsplit方法。你可以用它來取出panelOne。然後,當你想再次顯示它,你只需再次拆分SplitterWindow

import wx 
class MainFrame(wx.Frame): 
    """""" 

    #---------------------------------------------------------------------- 
    def __init__(self): 
     wx.Frame.__init__(self, None, title="test", size=(800,600)) 

     self.splitter = wx.SplitterWindow(self, wx.ID_ANY) 
     self.panelOne = wx.Panel(self.splitter,1) 
     self.panelTwo = wx.Panel(self.splitter,1) 
     self.panelOne.SetBackgroundColour('sky blue') 
     self.panelTwo.SetBackgroundColour('pink') 
     self.splitter.SplitHorizontally(self.panelOne, self.panelTwo) 
     self.splitter.SetMinimumPaneSize(20) 

     self.buttonpanel = wx.Panel(self, 1) 
     self.buttonpanel.SetBackgroundColour('white') 
     self.mybutton = wx.Button(self.buttonpanel,label = "Hide") 
     self.Bind(wx.EVT_BUTTON, self.show_hide, self.mybutton) 

     self.sizer = wx.BoxSizer(wx.VERTICAL) 
     self.sizer.Add(self.splitter, 2, wx.EXPAND) 
     self.sizer.Add(self.buttonpanel, 0, wx.EXPAND) 
     self.SetSizer(self.sizer) 

    def show_hide(self, event): 
     label = self.mybutton.GetLabel() 
     if label == "Hide": 
      ### How to Hide panelOne ?? 
      self.mybutton.SetLabel("Show") 
      self.splitter.Unsplit(self.panelOne) 
     if label == "Show": 
      ### How to Show panelOne ?? 
      self.splitter.SplitHorizontally(self.panelOne, self.panelTwo) 
      self.mybutton.SetLabel("Hide") 



if __name__ == "__main__": 
    app = wx.App(False) 
    frame = MainFrame() 
    frame.Show() 
    app.MainLoop() 

注意:您已經離開調用MainLoop在代碼的末尾。這使您的示例無法運行。

+0

感謝您的回答! – Deepa