获取WxPython面板项以进行扩展 [英] Getting a WxPython panel item to expand

查看:100
本文介绍了获取WxPython面板项以进行扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WxPython框架,其中包含单个项目,例如:

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

我有一个包含几个面板的框架,包括这个面板,尺寸由尺寸确定. 我希望此StaticText扩展. 使用仅包含文本的BoxSizer并设置wx.EXPAND标志可以解决问题,但是仅对一项使用sizer似乎很愚蠢.

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?

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

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

我刚刚意识到,创建带有一个项目的BoxSizer不适用于wx.VERTICAL:

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

嗯,我刚刚发现必须使用比例才能使装箱机以两种方式增长. (即在BoxSizer.Add的调用中将0替换为1.)

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

推荐答案

如果wx.Frame只有一个孩子,它将自动执行此操作.但是,wx.Panel不会自动执行此操作.您无法使用上浆器.如果您发现自己做得很多,只需创建一个便捷功能即可:

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

我在其中添加了padding属性作为额外的奖励.随意使用或抛弃它.

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

这篇关于获取WxPython面板项以进行扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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