Skip to content Skip to sidebar Skip to footer

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(): out

Solution 1:

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")
    foriinrange(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()

Solution 2:

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.

Post a Comment for "Wxpython — Staticbox Sizer Resizing Static Boxes"