Question

I am trying to nest an sizer with horizontal layout inside the vertical layout sizer of a notebookPage panel.

I took some inspiration from wxPython: Resizing a notebook within a notebook but cannot make it work properly.

Instead of getting this layout

Tonnage limit (in tons): [3000]
[x] Enable filter on matchings categories

Everything is mangled on a single line in the panel

I am running this on Win7, python 2.6.6, wx 2.8.12.1

Here is the relevant code:

import sys
import os


import wx

class MainWindow(wx.Frame):
    def __init__(self,parent,title):
        self.ID_LOAD=101
        self.ID_SAVE=102

        # based on a frame, so set up the frame
        wx.Frame.__init__(self,parent,wx.ID_ANY, title, size=(640,480))

        self.CreateStatusBar()

        self.tab = wx.Notebook(self, -1, style=(wx.NB_TOP))
        self.globPanel = wx.NotebookPage(self.tab, -1)
        self.orderPanel = wx.NotebookPage(self.tab, -1)
        self.slabsPanel = wx.NotebookPage(self.tab, -1)
        self.tab.AddPage(self.globPanel, "Global filter")
        self.tab.AddPage(self.orderPanel, "Orders filter")
        self.tab.AddPage(self.slabsPanel, "Slabs filter")

        self.toolbar = wx.ToolBar(self, wx.ID_ANY, style=wx.TB_HORIZONTAL)
        self.toolbar.SetToolBitmapSize((20,20))
        load_ico = wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_TOOLBAR, (20,20))
        self.toolbar.AddLabelTool(self.ID_LOAD, "Load", load_ico, bmpDisabled=wx.NullBitmap, shortHelp='Load', longHelp="Loads a text file to the filter editor")
        save_ico = wx.ArtProvider.GetBitmap(wx.ART_FILE_SAVE, wx.ART_TOOLBAR, (20,20))
        self.toolbar.AddLabelTool(self.ID_SAVE, "Save", save_ico, bmpDisabled=wx.NullBitmap, shortHelp='Save', longHelp="Saves the contents of the filter editor")
        self.toolbar.AddSeparator()

        self.tonnageStatic = wx.StaticText(self.globPanel, wx.ID_ANY, "Tonnage limit (in tons): ")
        self.tonnageEdit = wx.TextCtrl(self.globPanel, wx.ID_ANY, '30000') 
        self.tonnageSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.tonnageSizer.Add(self.tonnageStatic, flag=wx.EXPAND | wx.RIGHT | wx.LEFT | wx.TOP | wx.BOTTOM, border=10)
        self.tonnageSizer.Add(self.tonnageEdit, flag=wx.EXPAND | wx.RIGHT | wx.LEFT | wx.TOP | wx.BOTTOM, border=10)

        self.filterCheck = wx.CheckBox(self.globPanel, wx.ID_ANY, "Enable filter on matchings categories")

        self.globSizer = wx.BoxSizer(wx.VERTICAL)
        self.globSizer.Add(self.tonnageSizer)
        self.globSizer.Add(wx.StaticLine(self), 0, wx.EXPAND)
        self.globSizer.Add(self.filterCheck, 0, wx.EXPAND)
        self.globPanel.SetSizer(self.globSizer)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.toolbar,0,wx.EXPAND)
        self.sizer.Add(wx.StaticLine(self), 0, wx.EXPAND)
        self.sizer.Add(self.tab, 1, wx.EXPAND)
        self.SetSizer(self.sizer)
        self.Layout()
        self.toolbar.Realize()

        # Show it !!!
        self.Show(1)            

def main():
    app = wx.PySimpleApp()
    view = MainWindow(None, "Input filter")
    app.MainLoop()

if __name__ == "__main__":
    main()

It is slighlty better like this, but I don't get the static line and only the first character of the checkox label is displayed

self.tonnageStatic = wx.StaticText(self.globPanel, wx.ID_ANY, "Tonnage limit (in tons): ")
self.tonnageEdit = wx.TextCtrl(self.globPanel, wx.ID_ANY, '30000') 
self.tonnageSizer = wx.BoxSizer(wx.HORIZONTAL)
self.tonnageSizer.Add(self.tonnageStatic, 0, flag=wx.EXPAND|wx.ALL)
self.tonnageSizer.Add(self.tonnageEdit, 0, flag=wx.EXPAND|wx.ALL)

self.filterCheck = wx.CheckBox(self.globPanel, wx.ID_ANY, "Enable filter on matchings categories")

self.globSizer = wx.BoxSizer(wx.VERTICAL)
self.globSizer.Add(self.tonnageSizer)
self.globSizer.Add(wx.StaticLine(self.globPanel), 0, wx.EXPAND)
self.globSizer.Add(self.filterCheck, 0, wx.EXPAND)
self.globPanel.SetSizer(self.globSizer)

self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.toolbar,0,wx.EXPAND)
self.sizer.Add(wx.StaticLine(self), 0, wx.EXPAND)
self.sizer.Add(self.tab, 1, wx.EXPAND)
self.SetSizer(self.sizer)
self.globPanel.Layout()
self.Layout()
self.toolbar.Realize()

# Show it !!!
self.Show(1) 
Was it helpful?

Solution

I just ran it on windows and if you call .Layout() on the globPanel after the self.Layout() line it should kick the sizers into shape :)

Also you can use wx.ALL which is the same as right, left, top and bottom

EDIT: I was curious why the weird behaviour and looked into a bit more. I looked at places I've used notebooks and I always use panels for the pages. If you change the NotebookPages to Panels you can do away with the Layout calls and it works as expected.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top