Panel中的填充和wxpython中的静态文本换行

时间:2016-08-17 06:14:04

标签: python user-interface layout wxpython wxwidgets

我希望有这样的布局

enter image description here

框架内部有一个主面板,左侧和右侧有空白填充,另一个子面板位于中间。一个静态文本元素,位于子面板的顶部,在更改窗口大小时应该正确包装。

我的代码段如下所示

import wx

class MyPanel(wx.Panel):
    def __init__(self, parent):
        super(MyPanel, self).__init__(parent)

        self.hsizer = wx.BoxSizer()
        self.hsizer.AddStretchSpacer()

        self.panel = wx.Panel(self)
        self.panel.SetSize((350, -1))

        self.vsizer = wx.BoxSizer(wx.VERTICAL)

        label = (
            "This is a long scentence that I want it wrapped properly, "
            "but it doesn't seem to work at work. "
            "I prefer if you guys can give any sugeestions or help. "
            "For Long Long Long Long Long scentence."
        )
        style = wx.ALIGN_LEFT
        self.static_text = wx.StaticText(self, label=label, style=style)
        self.vsizer.Add(self.static_text, flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
        self.static_text.Wrap(self.panel.Size[0])
        self.static_text.SetAutoLayout(True)

        self.panel.SetSizer(self.vsizer)
        self.hsizer.Add(self.panel, flag=wx.ALIGN_CENTER)
        self.hsizer.AddStretchSpacer()
        self.SetSizer(self.hsizer)
        self.Layout()
        print self.Size
        print self.panel.Size

if __name__ == '__main__':
    app = wx.App()
    frame = wx.Frame(None)
    sizer = wx.BoxSizer()
    sizer.Add(MyPanel(frame), flag=wx.EXPAND)
    frame.SetSizer(sizer)
    frame.Show()
    app.MainLoop()

目前,不仅子面板不在中间,而且在更改窗口大小时,静态文本中的文本也无法正确包装。有什么建议让它发挥作用吗?

1 个答案:

答案 0 :(得分:4)

在尝试使用sizer实现布局时设置一些背景颜色通常很有帮助,这样很容易可视化边界的位置,以及它们在调整大小时的行为方式。另一个有用的工具是Widget Inspection Tool。例如:

import wx

class MyPanel(wx.Panel):
    def __init__(self, parent):
        super(MyPanel, self).__init__(parent)
        self.SetBackgroundColour('pink')

        self.hsizer = wx.BoxSizer()
        self.hsizer.AddStretchSpacer()

        self.panel = wx.Panel(self)
        self.panel.SetSize((350, -1))
        self.panel.SetBackgroundColour('green')

        self.vsizer = wx.BoxSizer(wx.VERTICAL)

        label = (
            "This is a long scentence that I want it wrapped properly, "
            "but it doesn't seem to work at work. "
            "I prefer if you guys can give any sugeestions or help. "
            "For Long Long Long Long Long scentence."
        )
        style = wx.ALIGN_LEFT
        self.static_text = wx.StaticText(self, label=label, style=style)
        self.static_text.SetBackgroundColour('yellow')

        self.vsizer.Add(self.static_text, flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
        self.static_text.Wrap(self.panel.Size[0])
        self.static_text.SetAutoLayout(True)

        self.panel.SetSizer(self.vsizer)
        self.hsizer.Add(self.panel, flag=wx.ALIGN_CENTER)
        self.hsizer.AddStretchSpacer()
        self.SetSizer(self.hsizer)

if __name__ == '__main__':
    app = wx.App()
    frame = wx.Frame(None)
    frame.SetBackgroundColour('sky blue')
    sizer = wx.BoxSizer()
    sizer.Add(MyPanel(frame), flag=wx.EXPAND)
    frame.SetSizer(sizer)
    frame.Show()

    import wx.lib.inspection
    wx.lib.inspection.InspectionTool().Show()

    app.MainLoop()

运行之后,我们可以看到事物与布局的草图不匹配,文本之类的东西在调整框架大小时不会改变大小或位置,而MyPanel(粉红色的)是也没有调整大小以填充框架。此外,从代码中可以看出,您希望文本是绿色面板(self.panel)的子项,但在运行时查看颜色显然不是。再看一下代码,我们看到它是用self作为父代而不是self.panel创建的。

因此,经过几次调整以解决这些问题后,我们最终得到了这段代码:

import wx

class MyPanel(wx.Panel):
    def __init__(self, parent):
        super(MyPanel, self).__init__(parent)
        self.SetBackgroundColour('pink')

        self.hsizer = wx.BoxSizer()
        self.hsizer.AddStretchSpacer()

        self.panel = wx.Panel(self)
        self.panel.SetSize((350, -1))
        self.panel.SetBackgroundColour('green')

        self.vsizer = wx.BoxSizer(wx.VERTICAL)

        label = (
            "This is a long scentence that I want it wrapped properly, "
            "but it doesn't seem to work at work. "
            "I prefer if you guys can give any sugeestions or help. "
            "For Long Long Long Long Long scentence."
        )
        style = wx.ALIGN_LEFT
        self.static_text = wx.StaticText(self.panel, label=label, style=style)
        self.static_text.SetBackgroundColour('yellow')

        self.vsizer.Add(self.static_text,
                        flag=wx.EXPAND | wx.ALL | wx.ALIGN_TOP, border=5)
        self.static_text.Wrap(self.panel.Size[0])

        self.panel.SetSizer(self.vsizer)
        self.hsizer.Add(self.panel, 1, flag=wx.EXPAND)
        self.hsizer.AddStretchSpacer()
        self.SetSizer(self.hsizer)

if __name__ == '__main__':
    app = wx.App()
    frame = wx.Frame(None)
    frame.SetBackgroundColour('sky blue')
    sizer = wx.BoxSizer()
    sizer.Add(MyPanel(frame), 1, flag=wx.EXPAND)
    frame.SetSizer(sizer)
    frame.Show()

    import wx.lib.inspection
    wx.lib.inspection.InspectionTool().Show()

    app.MainLoop()

最后,如果你希望在大小比350更小的情况下重新包装文本,那么你需要做一些事情,比如捕获EVT_SIZE事件,并在需要时再次使用不同的大小调用Wrap。