The wx.ALIGN* flags

Hi everyone!

Why some wx.ALIGN* flags are skipped?

Is the simultaneous use of these flags contradictory?
wx.ALIGN_LEFT | wx.ALIGN_RIGHT
or
wx.ALIGN_TOP | wx.ALIGN_BOTTOM

import wx

app = wx.App()
master = wx.Frame(None)
master.SetBackgroundColour("white")
gbs = wx.GridBagSizer(2,2)
flagcol = {0: wx.ALIGN_LEFT, 1: wx.ALIGN_LEFT | wx.ALIGN_RIGHT, 2: wx.ALIGN_RIGHT}
flagrow = {0: wx.ALIGN_TOP,  1: wx.ALIGN_TOP | wx.ALIGN_BOTTOM, 2: wx.ALIGN_BOTTOM}

for r in range(3):
    for c in range(3):
        p = wx.Panel(parent=master, size=wx.Size(50, 50))
        p.SetBackgroundColour((20*c*r,50*r,80*c))
        gbs.Add(item=p, pos=(r, c), flag=flagcol[c] | flagrow[r])
        if not gbs.IsColGrowable(c):
            gbs.AddGrowableCol(c, 1)
    gbs.AddGrowableRow(r, 1)

master.SetSizerAndFit(gbs)
master.Layout()
master.Show()
app.MainLoop()

Best regards!

More or less. What probably actually happens is that whichever flag is checked first in the sizer’s layout code wins. The results of this are a little more clear if you use the widget inspection tool to highlight the sizer and its items:

wit

The alignment flags just specify how the item is positioned within the space allocated to it by the sizer. It doesn’t attach the item to a side, or cause the item to be resized in any way, which seems to be what you might have been expecting. Using the wx.ALIGN_CENTER_* flags will probably give you something closer to what you are expecting.

flagcol = {0: wx.ALIGN_LEFT, 1: wx.ALIGN_CENTER_HORIZONTAL, 2: wx.ALIGN_RIGHT}
flagrow = {0: wx.ALIGN_TOP,  1: wx.ALIGN_CENTER_VERTICAL, 2: wx.ALIGN_BOTTOM}

wit2

Adding the wx.EXPAND flag will cause the item to fill the cell given to it by the sizer, which isn’t quite what you’re showing in your expected layout image. There isn’t a way to expand an item just vertically or just horizontally in a grid sizer, but you can accomplish the same thing by nesting it in a box sizer and then putting that sizer as the item in the grid sizer. Doing that in this example is left as an exercise for the reader. :wink: