wxPython — StaticBox Sizer 调整静态框的大小 [英] wxPython — StaticBox Sizer resizing Static Boxes

查看:21
本文介绍了wxPython — StaticBox Sizer 调整静态框的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码的一个最小示例,它应该自己运行——按原样运行,一切都按照我想要的方式布置,3 行节点均匀分布:

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?

推荐答案

将文本小部件添加到 6 个不同的 sizer 从来都不是一个好主意.我建议为每个 sizer 创建一个 StaticText 的新实例.重叠的原因是你在使用StaticBoxsizers时需要增加你的vgap.

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.

这是代码的更新版本:

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

这篇关于wxPython — StaticBox Sizer 调整静态框的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆