让一个WxPython面板项目展开[英] Getting a WxPython panel item to expand

本文是小编为大家收集整理的关于让一个WxPython面板项目展开的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个包含一个项目的wxpython框架,例如:

class Panel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.text = wx.StaticText(self, label='Panel 1')

我有一个包含多个面板的框架,包括该面板,尺寸由Sizers统治. 我希望这个StaticText扩展. 使用仅包含文本并设置wx.EXPAND标志的BoxSizer BoxSizer可以做到这一点,但是仅对一个项目使用sizer似乎很愚蠢.

任何更简单的解决方案?

(我可以直接将StaticText直接添加到父框架的sizer,但是对于我的设计,直接从框架开始更有意义.)


我只是意识到,当创建一个项目BoxSizer时,一项不适用于wx.VERTICAL:

class Panel1(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.BackgroundColour = 'red'
        sizer = wx.BoxSizer(wx.VERTICAL)
        self.Sizer = sizer
        self.list = wx.ListBox(self, choices=[str(i) for i in xrange(100)])
        sizer.Add(self.list, 0, wx.EXPAND)
        sizer.Fit(self)

当然可以使用一个项目,但是如果我想以后垂直添加一个项目并使它们都扩展(例如,当用户的窗口扩展时)怎么办?

)?

编辑:啊,我刚刚发现必须使用比例来使盒装器以两种方式生长. (即,将0替换为BoxSizer.Add的呼叫.)

推荐答案

a wx.Frame如果只有一个孩子,将自动这样做.但是,A wx.Panel不会自动执行此操作.您使用sizer卡住了.如果您发现自己做很多事情,只需做一个便利功能:

def expanded(widget, padding=0):
    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(widget, 1, wx.EXPAND|wx.ALL, padding)
    return sizer

class Panel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.text = wx.StaticText(self, label='Panel 1')
        self.SetSizer(expanded(self.text))

我将填充属性扔到那里是一个额外的奖励.随意使用或抛弃它.

本文地址:https://www.itbaoku.cn/post/2091163.html

问题描述

I have a WxPython frame containing a single item, such as:

class Panel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.text = wx.StaticText(self, label='Panel 1')

I have a frame containing several panels, including this one, and dimensions are ruled by sizers. I would like this StaticText to expand. Using a BoxSizer containing just the text and setting the wx.EXPAND flag does the trick, but it seems silly to use a sizer just for one item.

Any simpler solution?

(I could just add the StaticText to the parent frame's sizer directly, but for my design it makes more sense to start with a frame directly.)


I just realized that when creating a BoxSizer with one item doesn't work with wx.VERTICAL:

class Panel1(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.BackgroundColour = 'red'
        sizer = wx.BoxSizer(wx.VERTICAL)
        self.Sizer = sizer
        self.list = wx.ListBox(self, choices=[str(i) for i in xrange(100)])
        sizer.Add(self.list, 0, wx.EXPAND)
        sizer.Fit(self)

Of course it's ok with one item, but what if I want to add an item vertically later and still make both of them expand (e.g. when the user's window is expanded)?

Edit: ah, I just found out that proportion must be used in order to make boxsizers grow in both ways. (i.e., replace 0 with 1 in BoxSizer.Add's call.)

推荐答案

A wx.Frame will automatically do this if it only has one child. However, a wx.Panel will not do this automatically. You're stuck using a sizer. If you find yourself doing it a lot, just make a convenience function:

def expanded(widget, padding=0):
    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(widget, 1, wx.EXPAND|wx.ALL, padding)
    return sizer

class Panel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.text = wx.StaticText(self, label='Panel 1')
        self.SetSizer(expanded(self.text))

I threw the padding attribute in there as an extra bonus. Feel free to use it or ditch it.