wxPython - StaticBox Sizer调整静态框的大小

时间:2015-03-10 12:21:13

标签: python user-interface wxpython

这是我自己应该运行的代码的最小示例 - 按原样运行,所有内容都按照我想要的方式布局,3行节点均匀分布:

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

但是,当我取消注释两条注释行并将其后的行更改为:

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

布局变得狭窄,节点垂直重叠等等。我无法弄清楚我在这里做错了什么,有没有人有建议?

2 个答案:

答案 0 :(得分:1)

这将是获得一些结果的最低必要变化:

    # 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)

您已将text添加到六个不同的大小调整器中(这不是一个好主意)。其次,您必须将StaticBoxSizer静态框添加到父级sizer。

下一个问题是,对于sizer flag=wxEXPAND已设置,但没有比例。因此它永远不会改变大小。

答案 1 :(得分:1)

将文本小部件添加到6个不同的大小调整器中绝不是一个好主意。我建议为每个sizer创建一个StaticText的新实例。重叠的原因是您在使用vgap时需要增加StaticBoxsizers

这是代码的更新版本:

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