linus72982
linus72982

Reputation: 1393

wxPython — StaticBox Sizer resizing Static Boxes

Here is a minimal example of my code that should run by itself -- run as is, everything is laid out how I want, 3 rows of nodes spaced evenly:

import wx

def createBoxes():
    outVSizer = wx.BoxSizer(wx.VERTICAL)
    outHSizer = wx.BoxSizer(wx.HORIZONTAL)
    outVSizer.AddStretchSpacer(1)
    outHSizer.AddStretchSpacer(1)
    sizer = wx.FlexGridSizer(rows=3, cols=3, vgap=35, hgap=20)

    box = {}
    boxSizer = {}
    text = wx.StaticText(panel, wx.ID_ANY, "This is a test")
    for i in range(6):
        box[i] = wx.StaticBox(panel, wx.ID_ANY, "testBox", size=(0,100))
        #boxSizer[i] = wx.StaticBoxSizer(box[i], wx.VERTICAL)
        #boxSizer[i].Add(text, proportion = 1, flag=wx.ALIGN_CENTER)
        sizer.Add(box[i], flag=wx.EXPAND)
    sizer.AddGrowableCol(0,1)
    sizer.AddGrowableCol(1,1)
    sizer.AddGrowableCol(2,1)

    outHSizer.Add(sizer, flag=wx.EXPAND, proportion=15)
    outHSizer.AddStretchSpacer(1)
    outVSizer.Add(outHSizer, flag=wx.EXPAND, proportion=15)
    panel.SetSizer(outVSizer)

app = wx.App()
frame = wx.Frame(None, size=(500,500))
panel = wx.Panel(frame)
createBoxes()
frame.Show()
app.MainLoop()

However, when I uncomment the two commented lines and change the line after those to:

sizer.Add(boxSizer[i], flag=wx.EXPAND)

The layout gets cramped and the nodes overlap vertically, etc. I can't figure out what I'm doing wrong here, does anyone have a suggestion?

Upvotes: 1

Views: 1655

Answers (2)

Mike Driscoll
Mike Driscoll

Reputation: 33071

Adding the text widget to 6 different sizers is never a good idea. I recommend creating a new instance of StaticText for each sizer. The reason for the overlap is that you need to increase your vgap when using StaticBoxsizers.

Here's an updated version of the code:

import wx

def createBoxes():
    outVSizer = wx.BoxSizer(wx.VERTICAL)
    outHSizer = wx.BoxSizer(wx.HORIZONTAL)
    outVSizer.AddStretchSpacer(1)
    outHSizer.AddStretchSpacer(1)
    sizer = wx.FlexGridSizer(rows=3, cols=3, vgap=55, hgap=20)

    box = {}
    boxSizer = {}
    #text = wx.StaticText(panel, wx.ID_ANY, "This is a test")
    for i in range(6):
        box[i] = wx.StaticBox(panel, wx.ID_ANY, "testBox", size=(0,100))
        text = wx.StaticText(panel, wx.ID_ANY, "This is a test")
        boxSizer[i] = wx.StaticBoxSizer(box[i], wx.VERTICAL)
        boxSizer[i].Add(text, proportion = 1, flag=wx.ALIGN_CENTER)
        sizer.Add(boxSizer[i], flag=wx.EXPAND)
    sizer.AddGrowableCol(0,1)
    sizer.AddGrowableCol(1,1)
    sizer.AddGrowableCol(2,1)

    outHSizer.Add(sizer, flag=wx.EXPAND, proportion=15)
    outHSizer.AddStretchSpacer(1)
    outVSizer.Add(outHSizer, flag=wx.EXPAND, proportion=15)
    panel.SetSizer(outVSizer)

app = wx.App()
frame = wx.Frame(None, size=(500,500))
panel = wx.Panel(frame)
createBoxes()
frame.Show()
app.MainLoop()

Upvotes: 2

nepix32
nepix32

Reputation: 3177

this will be the minimum necessary changes to get some results:

    # text = wx.StaticText(panel, wx.ID_ANY, "This is a test")
    for i in range(6):
        box[i] = wx.StaticBox(panel, wx.ID_ANY, "testBox", size=(0,100))
        # change I
        text = wx.StaticText(panel, wx.ID_ANY, "This is a test")
        boxSizer[i] = wx.StaticBoxSizer(box[i], wx.VERTICAL)
        boxSizer[i].Add(text, proportion = 1, flag=wx.ALIGN_CENTER)
        # sizer.Add(box[i], flag=wx.EXPAND)
        # change II
        sizer.Add(boxSizer[i], flag=wx.EXPAND)

You have added text to six different sizers (which is not a good idea). And second, you have to add the StaticBoxSizer and not the static box to the parent sizer.

The next issue is that for sizer the flag=wxEXPAND is set, but no proportion. Therefore it will never change size.

Upvotes: 2

Related Questions