wxPython:在两个boxsizer中对齐元素

时间:2015-04-08 13:38:20

标签: wxpython wxwidgets

我想调整盒式传感器的两个元素,即:" BIG BIG STRINNNNNNGGGGGGGG"和"测量"所以他们就像这样在同一列下:

hello world                                Measurement
oh no worldBIG BIG STRINNNNNNGGGGGGGGGGGGGGGGGGGGGGGGG

如何使用以下代码实现这一目标?

阅读wxPython文档时,它说:

  

ALIGN *标志允许您指定项目内的对齐方式   大小分配给它的空间,如果有的话调整边界。

然而,在这种情况下并非如此。

import wx
from imgtec import codescape
import wx.lib.inspection

class MyRegion(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, title="My Region")
        b1=wx.BoxSizer(orient=wx.HORIZONTAL)
        b2=wx.BoxSizer(orient=wx.HORIZONTAL)

        d1 = wx.StaticText(self, label="hello world")
        b1.Add(d1)
        d2 = wx.StaticText(self, label="Measurement", style=wx.ALIGN_RIGHT)
        b1.Add(d2,
               flag=wx.ALIGN_RIGHT | wx.EXPAND)

        c1 = wx.StaticText(self, label="oh no world")
        b2.Add(c1)
        c2 = wx.StaticText(self, label="BIG BIG STRINNNNNNGGGGGGGGGGGGGGGGGGGGGGGGG",style=wx.ALIGN_RIGHT)
        b2.Add(c2,
           flag=wx.ALIGN_RIGHT| wx.EXPAND )

        sizer = wx.GridBagSizer()
        sizer.Add(item=b2, pos=(0,0), flag=wx.EXPAND)
        sizer.Add(item=b1, pos=(1,0), flag=wx.EXPAND)
        self.SetSizer(sizer)


if __name__ == "__main__":
    app = wx.App()
    frame = MyRegion(None)
    frame.Show()
    wx.lib.inspection.InspectionTool().Show()
    app.MainLoop()

2 个答案:

答案 0 :(得分:1)

您只需在d1d2之间添加间隔符:

import wx
import wx.lib.inspection

class MyRegion(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, title="My Region")
        b1=wx.BoxSizer(orient=wx.HORIZONTAL)
        b2=wx.BoxSizer(orient=wx.HORIZONTAL)

        d1 = wx.StaticText(self, label="hello world")
        b1.Add(d1)

        b1.AddStretchSpacer()  # <-- ADD THE SPACER HERE

        d2 = wx.StaticText(self, label="Measurement", style=wx.ALIGN_RIGHT)
        b1.Add(d2,
               flag=wx.ALIGN_RIGHT | wx.EXPAND)

        c1 = wx.StaticText(self, label="oh no world")
        b2.Add(c1)
        c2 = wx.StaticText(self, label="BIG BIG STRINNNNNNGGGGGGGGGGGGGGGGGGGGGGGGG",style=wx.ALIGN_RIGHT)
        b2.Add(c2,
           flag=wx.ALIGN_RIGHT| wx.EXPAND )

        sizer = wx.GridBagSizer()
        sizer.Add(item=b2, pos=(0,0), flag=wx.EXPAND)
        sizer.Add(item=b1, pos=(1,0), flag=wx.EXPAND)
        self.SetSizer(sizer)


if __name__ == "__main__":
    app = wx.App()
    frame = MyRegion(None)
    frame.Show()
    wx.lib.inspection.InspectionTool().Show()
    app.MainLoop()

答案 1 :(得分:1)

Mike Driscoll的答案已经说明了正确的方法,我只想补充说,有一个简单的规则要记住:对齐标志只能在横向大小方向上工作,即垂直方向用于水平框大小调整器

wxWidgets的未来版本会在水平sizer中使用wxALIGN_RIGHT时出错(并且类似地,wxALIGN_TOP或垂直对齐内的另一个垂直对齐)因为这可能从不

您可以在2D大小调整器中使用两个方向的对齐标记,例如wx[Flex]GridSizer