简体   繁体   中英

WxPython Window Splitter

I am trying to use window splitter to get 6 different subwindows. Three columns and two rows. So far I am trying to use nested splitters, but it is not working.

This is the code I have:

import wx

########################################################################
class RandomPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent, color):
        """Constructor"""
        wx.Panel.__init__(self, parent)
        self.SetBackgroundColour(color)

########################################################################
class MainPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)

        topSplitter = wx.SplitterWindow(self)
        vSplitter = wx.SplitterWindow(topSplitter)


        panelOne = RandomPanel(vSplitter, "blue")
        panelTwo = RandomPanel(vSplitter, "red")
        vSplitter.SplitVertically(panelOne, panelTwo)
        vSplitter.SetSashGravity(0.5)

        panelThree = RandomPanel(topSplitter, "green")
        topSplitter.SplitHorizontally(vSplitter, panelThree)
        topSplitter.SetSashGravity(0.5)

        panelFour=RandomPanel(hSplitter, "yellow")
        hSplitter.SplitVertically(topSplitter, panelFour)
        hSplitter.SetSashGravity(0.5)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(topSplitter, 1, wx.EXPAND)
        self.SetSizer(sizer)
#
if __name__ == "__main__":
    app = wx.App(False)
    frame = MainFrame()
    app.MainLoop()

----------------------------------------------------------------------

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

For this I am just trying to get 4 subwindows not 6, and I still get an error.

You might want to check out the MultiSplitterWindow widget:

It allows you to have more than two windows in a splitter window widget. I actually wrote a bit about the various splitter windows that wxPython supports over on my blog , so you might want to check that out too.

Anyway, here is a simple example that uses that widget:

import wx
from wx.lib.splitter import MultiSplitterWindow

class SplitterPanel(wx.Panel):

    def __init__(self, parent, color):
        """Constructor"""
        wx.Panel.__init__(self, parent)
        main_sizer = wx.BoxSizer(wx.VERTICAL)

        splitter = MultiSplitterWindow(self, style=wx.SP_LIVE_UPDATE)
        for col in range(3):
            panel = wx.Panel(splitter, size=(200, 200))
            panel.SetBackgroundColour(color)
            splitter.AppendWindow(panel)

        main_sizer.Add(splitter, 1, wx.ALL|wx.EXPAND)
        self.SetSizer(main_sizer)

class MainFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, title='Splitters')
        panel = wx.Panel(self)
        main_sizer = wx.BoxSizer(wx.VERTICAL)

        splitter_panel = SplitterPanel(panel, color='blue')
        main_sizer.Add(splitter_panel, 1, wx.ALL|wx.EXPAND)

        splitter_panel_two = SplitterPanel(panel, color='red')
        main_sizer.Add(splitter_panel_two, 1, wx.ALL|wx.EXPAND)

        panel.SetSizer(main_sizer)

        self.Show()


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM