Make sizers a little bit better

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Make sizers a little bit better

Boštjan Mejak-4
So, I've got this code mess that I think it can get a little bit tidier. I have 5 elements that I want to layout on the panel, but I must use 3 sizers to make it work.

This is the order:

element 1        element 2
element 3        element 4
            element 5


And this is the code to achieve this:

sizer1 = wx.BoxSizer(orient=wx.HORIZONTAL)
sizer2 = wx.BoxSizer(orient=wx.HORIZONTAL)
sizer3 = wx.BoxSizer(orient=wx.VERTICAL)

sizer1.Add(window=labelOfTextFieldForURL,
                  border=5,
                  flag=wx.ALL | wx.ALIGN_CENTER)
sizer1.Add(window=self.textFieldForURL,
                  border=5,
                  flag=wx.ALL | wx.ALIGN_CENTER)

sizer2.Add(window=labelOfTextFieldForSearchTerm,
                  border=5,
                  flag=wx.ALL | wx.ALIGN_CENTER)
sizer2.Add(window=self.textFieldForSearchTerm,
                  border=5,
                  flag=wx.ALL | wx.ALIGN_CENTER)

sizer3.AddStretchSpacer()
sizer3.Add(sizer=sizer1,
                  border=5,
                  flag=wx.ALL | wx.ALIGN_CENTER)
sizer3.Add(sizer=sizer2,
                  border=5,
                  flag=wx.ALL | wx.ALIGN_CENTER)
sizer3.Add(window=buttonCount,
                  border=5,
                  flag=wx.ALL | wx.ALIGN_CENTER)
sizer3.AddStretchSpacer()

panel.SetSizer(sizer3)
panel.Layout()


There must be a better way to achieve this. May I get your expertise?

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Make sizers a little bit better

Matt Newville
On Wed, Mar 8, 2017 at 9:27 AM, Boštjan Mejak <[hidden email]> wrote:
So, I've got this code mess that I think it can get a little bit tidier. I have 5 elements that I want to layout on the panel, but I must use 3 sizers to make it work.

This is the order:

element 1        element 2
element 3        element 4
            element 5



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. 

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Make sizers a little bit better

Chris Barker - NOAA Federal
Depending what you are going for, a GridSizer for the top 4 would work well. 

But actually, nothing wrong with what you have.

-CHB

On Mar 8, 2017, at 7:46 AM, Matt Newville <[hidden email]> wrote:

On Wed, Mar 8, 2017 at 9:27 AM, Boštjan Mejak <[hidden email]> wrote:
So, I've got this code mess that I think it can get a little bit tidier. I have 5 elements that I want to layout on the panel, but I must use 3 sizers to make it work.

This is the order:

element 1        element 2
element 3        element 4
            element 5



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. 

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Make sizers a little bit better

Anders Munch
In reply to this post by Boštjan Mejak-4
Matt Newville, [hidden email], wrote:
> Boštjan Mejak <[hidden email]> wrote:
>> 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 (https://github.com/AndersMunch/wxWize) 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

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Make sizers a little bit better

Boštjan Mejak-4
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?

On Wed, Mar 8, 2017 at 5:19 PM Anders Munch <[hidden email]> wrote:
Matt Newville, [hidden email], wrote:
> Boštjan Mejak <[hidden email]> wrote:
>> 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 (https://github.com/AndersMunch/wxWize) 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

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Make sizers a little bit better

Matt Newville


On Wed, Mar 8, 2017 at 11:07 AM, Boštjan Mejak <[hidden email]> 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?

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

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.