Search code examples
alignmentwxpython

wxPython - Can't make wx.StaticText align inside a wx.StaticBoxSizer


how it going?

I'm trying to align two wx.StaticText inside a horizontal box, which is inside a vertical wx.StaticBoxSizer. See for yourself. I guess the code says what I'm trying to do.

Thanks!

import wx

class Frame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent)

        sizer = wx.StaticBoxSizer(wx.VERTICAL, self)
        left = wx.StaticText(self, -1, 'left', style=wx.ALIGN_LEFT)
        right = wx.StaticText(self, -1, 'right', style=wx.ALIGN_RIGHT)

        hBox = wx.BoxSizer(wx.HORIZONTAL)
        hBox.Add(left)
        hBox.Add(right)

        sizer.Add(hBox)
        self.SetSizerAndFit(sizer)

app = wx.App()
frame = Frame(None).Show()
app.MainLoop()

Solution

  • Ever since the removal of wx.ALIGN_LEFT and wx.ALIGN_RIGHT in horizontal boxsizers and the introduction of:
    wx._core.wxAssertionError: C++ assertion "!(flags & wxALIGN_RIGHT)" failed at /tmp/pip-install-8ko13ycp/wxpython/ext/wxWidgets/src/common/sizer.cpp(2168) in DoInsert(): Horizontal alignment flags are ignored in horizontal sizers,

    it's an issue.

    Another option is to use the spacing in the sizer instruction to the Left and/or Right. e.g.

    import wx
    
    class Frame(wx.Frame):
        def __init__(self, parent):
            wx.Frame.__init__(self, parent)
    
            sizer = wx.StaticBoxSizer(wx.VERTICAL, self, "Test")
            left = wx.StaticText(self, -1, 'left')
            right = wx.StaticText(self, -1, 'right')
    
            hBox = wx.BoxSizer(wx.HORIZONTAL)
            hBox.Add(left, 0, wx.RIGHT, 100)
            hBox.Add(right, 0, wx.LEFT, 100)
    
            sizer.Add(hBox)
            self.SetSizerAndFit(sizer)
    
    app = wx.App()
    frame = Frame(None).Show()
    app.MainLoop()
    

    enter image description here