Search code examples
wxpythonwxwidgets

wxPython: Aligning the elements within two boxsizers


I want to align two of the elements of the boxsizers, namely: "BIG BIG STRINNNNNNGGGGGGGGG" and "Measurement" so that they are under the same column like so:

hello world                                Measurement
oh no worldBIG BIG STRINNNNNNGGGGGGGGGGGGGGGGGGGGGGGGG

How can I achieve this by using the code below?

Reading the wxPython documentation it says the following:

The ALIGN* flags allow you to specify the alignment of the item within the space allotted to it by the sizer, adjusted for the border if any.

However that is not true in this case.

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

Solution

  • You just need to add a spacer between d1 and d2:

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