wxpython:隐藏splitterWindow面板之一

时间:2022-09-17 07:13:14

I have a frame containing wx.SplitterWindow with two panels as its children. I want to hide one of the panel with a button(show/hide button) click i.e, First panel should hide and the second panel should occupy the whole frame with the sash gone. Clicking the button again should show the hidden panel, and the sash back in place. Is this possible?

我有一个包含wx.SplitterWindow的框架,其中有两个面板作为子框架。我想用一个按钮(显示/隐藏按钮)隐藏其中一个面板,即第一个面板应该隐藏,第二个面板应该占用整个框架,窗框不见了。再次单击该按钮应显示隐藏的面板,并将窗扇重新放回原位。这可能吗?

I have searched the documentation, and there seems to be no specific method to do this? How can this be achieved.

我搜索了文档,似乎没有具体的方法来做到这一点?如何实现这一目标。

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 个解决方案

#1


After reading the documentation for a few seconds, I noticed the Unsplit method. You can use that to take out panelOne. Then when you want to Show it again, you just split the SplitterWindow again:

在阅读文档几秒钟后,我注意到了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()

Note: You had left off the call to MainLoop at the end of the code. This made your example un-runnable.

注意:您在代码末尾停止了对MainLoop的调用。这使您的示例无法运行。

#1


After reading the documentation for a few seconds, I noticed the Unsplit method. You can use that to take out panelOne. Then when you want to Show it again, you just split the SplitterWindow again:

在阅读文档几秒钟后,我注意到了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()

Note: You had left off the call to MainLoop at the end of the code. This made your example un-runnable.

注意:您在代码末尾停止了对MainLoop的调用。这使您的示例无法运行。