wxPython:BoxSizer 中的项目不能水平扩展,只能垂直扩展 [英] wxPython: Items in BoxSizer don't expand horizontally, only vertically

查看:16
本文介绍了wxPython:BoxSizer 中的项目不能水平扩展,只能垂直扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在不同的 sizer 中有几个按钮,它们以我想要的方式展开.但是,当我将父级添加到用于为框架中的所有元素添加边框的新 wx.BoxSizer 时,已添加的 sizer 在垂直方向上正常运行,但在水平方向上无法正常运行.

I have several buttons in various sizers and they expand in the way that I want them to. However, when I add the parent to a new wx.BoxSizer that is used to add a border around all the elements in the frame, the sizer that has been added functions correctly vertically, but not horizontally.

下面的代码演示了这个问题:

The following code demonstrates the problem:

#! /usr/bin/env python

import wx
import webbrowser

class App(wx.App):


    def OnInit(self):
       frame = MainFrame()
       frame.Show()
       self.SetTopWindow(frame)
       return True


class MainFrame(wx.Frame):

    title = 'Title'


    def __init__(self):
        wx.Frame.__init__(self, None, -1, self.title)

        panel = wx.Panel(self)

        #icon = wx.Icon('icon.png', wx.BITMAP_TYPE_PNG)
        #self.SetIcon(icon)

        sizer = wx.FlexGridSizer(rows=2, cols=1, vgap=10, hgap=10)

        button1 = wx.Button(panel, -1, 'BUTTON')
        sizer.Add(button1, 0, wx.EXPAND)

        buttonSizer = wx.FlexGridSizer(rows=1, cols=4, vgap=10, hgap=5)

        buttonDelete = wx.Button(panel, -1, 'Delete')
        buttonSizer.Add(buttonDelete, 0, 0)

        buttonEdit = wx.Button(panel, -1, 'Edit')
        buttonSizer.Add(buttonEdit, 0, 0)

        buttonNew = wx.Button(panel, -1, 'New')
        buttonSizer.Add(buttonNew, 0, 0)

        buttonSizer.AddGrowableCol(0, 0)

        sizer.Add(buttonSizer, 0, wx.EXPAND|wx.HORIZONTAL)

        sizer.AddGrowableCol(0, 0)
        sizer.AddGrowableRow(0, 0)

        mainSizer = wx.BoxSizer(wx.EXPAND)
        mainSizer.Add(sizer, 0, wx.EXPAND|wx.ALL, 10)

        #panel.SetSizerAndFit(sizer)
        #sizer.SetSizeHints(self)
        panel.SetSizerAndFit(mainSizer)
        mainSizer.SetSizeHints(self)


if __name__ == '__main__':
    app = App(False)
    app.MainLoop()

注释掉 5758 行并取消注释 5556 行会删除额外的 BoxSizer 并展示如何我希望一切正常(当然没有空格).

Commenting out lines 57 and 58 and uncommenting lines 55 and 56 removes the extra BoxSizer and shows how I expect everything to function (without the whitespace of course).

我完全被这个问题困住了,仍然不知道如何解决它.

I am completely stuck with this problem and still have no clue as to how to fix it.

推荐答案

首先,您错误地传递了一些标志.BoxSizer 采用 wx.HORIZONTAL 或 wx.VERTICAL,而不是 wx.EXPAND.sizer.Add 不带 wx.HORIZONTAL.

First of all, you're passing some flags incorrectly. BoxSizer takes wx.HORIZONTAL or wx.VERTICAL, not wx.EXPAND. sizer.Add does not take wx.HORIZONTAL.

如果你有一个 VERTICAL BoxSizer,wx.EXPAND 将使控件水平填充,而 1 或更多的比例(Add 的第二个参数)将使控件垂直填充.HORIZONTAL BoxSizer 的情况正好相反.

If you have a VERTICAL BoxSizer, wx.EXPAND will make the control fill horizontally, while a proportion of 1 or more (second argument to Add) will make the control fill vertically. It's the opposite for HORIZONTAL BoxSizers.

sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(widget1, 0, wx.EXPAND)
sizer.Add(widget2, 1)

widget1 将水平扩展.widget2 将垂直展开.

widget1 will expand horizontally. widget2 will expand vertically.

如果您将一个 sizer 放在另一个 sizer 中,您需要确保设置其比例和 EXPAND 标志,以便其内部按照您希望的方式增长.

If you put a sizer in another sizer, you need to be sure to have its proportion and EXPAND flags set so that its insides will grow how you want them to.

剩下的交给你.

这篇关于wxPython:BoxSizer 中的项目不能水平扩展,只能垂直扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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