Search code examples
wxwidgetswxpython

wxpython add splitter window to notebook page


I have adapted this for my own use:

import wx
import wx.grid as gridlib

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

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

        grid = gridlib.Grid(self)
        grid.CreateGrid(25,12)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(grid, 0, wx.EXPAND)
        self.SetSizer(sizer)

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

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

########################################################################
class MyForm(wx.Frame):

    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, title="Splitter Tutorial")

        splitter = wx.SplitterWindow(self)
        leftP = LeftPanel(splitter)
        rightP = RightPanel(splitter)

        # split the window
        splitter.SplitVertically(leftP, rightP)
        splitter.SetMinimumPaneSize(20)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(splitter, 1, wx.EXPAND)
        self.SetSizer(sizer)

#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

Now I want to add it as a page in a simple wxNotebook. Normally the notebook page would be set up something like:

p = wx.Panel(self)
nb = wx.Notebook(p)
page1 = module_name.module_Class(nb)
nb.AddPage(page1, "Tab label")

The splitter has two (three in my case) panel Classes but obviously I can't just call one of them. Can someone please explain how I can wrap it all up? Thanks.


Solution

  • You just need to put the splitter widget on a panel in its own class. Then you can add your panel with the splitter in it to a notebook widget. Here's an example:

    import wx
    import wx.grid as gridlib
    
    ########################################################################
    class LeftPanel(wx.Panel):
        """"""
    
        #----------------------------------------------------------------------
        def __init__(self, parent):
            """Constructor"""
            wx.Panel.__init__(self, parent=parent)
    
            grid = gridlib.Grid(self)
            grid.CreateGrid(25,12)
    
            sizer = wx.BoxSizer(wx.VERTICAL)
            sizer.Add(grid, 0, wx.EXPAND)
            self.SetSizer(sizer)
    
    ########################################################################
    class RightPanel(wx.Panel):
        """"""
    
        #----------------------------------------------------------------------
        def __init__(self, parent):
            """Constructor"""
            wx.Panel.__init__(self, parent=parent)
            txt = wx.TextCtrl(self)
    
    ########################################################################
    class SplitterPanel(wx.Panel):
        """"""
    
        #----------------------------------------------------------------------
        def __init__(self, parent):
            """Constructor"""
            wx.Panel.__init__(self, parent)
    
            splitter = wx.SplitterWindow(self)
            leftP = LeftPanel(splitter)
            rightP = RightPanel(splitter)
            # split the window
            splitter.SplitVertically(leftP, rightP)
            splitter.SetMinimumPaneSize(20)
    
            sizer = wx.BoxSizer(wx.VERTICAL)
            sizer.Add(splitter, 1, wx.EXPAND)
            self.SetSizer(sizer)
    
    
    ########################################################################
    class MyForm(wx.Frame):
    
        #----------------------------------------------------------------------
        def __init__(self):
            wx.Frame.__init__(self, None, title="Notebook Splitter Tutorial")
    
            panel = wx.Panel(self)
    
            notebook = wx.Notebook(panel)
            splitter = SplitterPanel(notebook)
            notebook.AddPage(splitter, 'Splitter')
    
            sizer = wx.BoxSizer(wx.VERTICAL)
            sizer.Add(notebook, 1, wx.ALL|wx.EXPAND, 5)
            panel.SetSizer(sizer)
    
    #----------------------------------------------------------------------
    # Run the program
    if __name__ == "__main__":
        app = wx.App(False)
        frame = MyForm()
        frame.Show()
        app.MainLoop()