Make sizers a little bit better

So, I've got this code mess that I think it can get a little bit tidier.

You might find a wx.GridBagSizer useful for this. The standard demo set has a nice example that shows how you can have widgets span multiple cells, make some cells growable, etc.

wxWize (GitHub - AndersMunch/wxWize: wxPython object builder library) can help cut down on the sizer boilerplate. Whether you use BoxSizer's or GridBagSizer.
You'd still need the same number of sizers, but it does away with the explicit .Add's, and the structure of the code comes to better match the structure of the sizer hierarchy, making the code easier to follow and refactor.

Here's a first approximation to Boštjan's design, as a complete runnable program:

import wx
import wize as iz
class MyFrame(wx.Frame):
    def __init__(self):
        with iz.Frame(init=self, title="Demo") as the_frame:
            with iz.BoxSizer(wx.VERTICAL, proportion=1):
                iz.StretchSpacer()
                with iz.BoxSizer(wx.HORIZONTAL, proportion=1):
                    iz.StretchSpacer()
                    iz.StaticText(u"element1")
                    iz.StaticText(u"element2")
                    iz.StretchSpacer()
                with iz.BoxSizer(wx.HORIZONTAL, proportion=1):
                    iz.StretchSpacer()
                    iz.StaticText(u"element3")
                    iz.StaticText(u"element4")
                    iz.StretchSpacer()
                with iz.BoxSizer(wx.HORIZONTAL, proportion=1):
                    iz.StretchSpacer()
                    iz.StaticText(u"element5")
                    iz.StretchSpacer()
                iz.StretchSpacer()
app = wx.App()
MyFrame().Show(True)
app.MainLoop()

I second the recommendation of GridBagSizer though. All those BoxSizers and StretchSpacer's can become a little fiddly.

Disclaimer: In case it isn't obvious, I am the author of wxWize.

regards, Anders

···

Matt Newville, matt.newville@gmail.com, wrote:

Boštjan Mejak <bostjan.xperia@gmail.com> wrote:

Well, "tidy" is in the eye of the beholder, but I would think that you
could probably get used to looking at something like

    sizer = wx.GridBagSizer(5, 5)
    style = wx.ALL | wx.ALIGN_CENTER

    sizer.Add(url_label, (0, 0), (1, 1), style)
    sizer.Add(url_text, (0, 1), (1, 1), style)
    sizer.Add(search_label, (1, 0), (1, 1), style)
    sizer.Add(search_text, (1, 1), (1, 1), style)
    sizer.Add(count_button, (2, 0), (1, 2), style)

    panel.SetSizer(sizer)

--Matt

···

On Wed, Mar 8, 2017 at 11:07 AM, Boštjan Mejak <bostjan.xperia@gmail.com> wrote:

My sizer code is so untidy, but I guess that with having an odd number of
items to lay out, there simply isn't no other way, right?