Har
Har

Reputation: 3918

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

Upvotes: 0

Views: 323

Answers (2)

VZ.
VZ.

Reputation: 22688

The answer by Mike Driscoll already shows the way to do it correctly, I'd just like to add that there is a simple rule to keep in mind: alignment flags only work in transversal sizer direction, i.e. vertically for horizontal box sizers.

Future versions of wxWidgets will give errors when wxALIGN_RIGHT is used inside a horizontal sizer (and, similarly, wxALIGN_TOP or another vertical alignment inside a vertical one) because this can never work.

You can use alignment flags in both directions in 2D sizers such as wx[Flex]GridSizer however.

Upvotes: 1

Mike Driscoll
Mike Driscoll

Reputation: 33071

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

Upvotes: 1

Related Questions