Hello,
I’m just starting out with sizers; I’ve hit a problem placing buttons using
a box sizer. I can place the buttons in a panel using absolute positioning
and then put that panel in a sizer but I can’t add the buttons directly to
the sizer.
Hello. First off, keep in mind that buttons are not special in regards to sizers. In fact, as far as I know, no visual object on the GUI is special with regards to sizers. They’ll manage the layout for any visible thing you put in them.
I am following a tutorial at http://www.zetcode.com/wxpython/layout/ and in
one of the examples (attached ‘tutorial_example.py’) they add buttons
directly to a sizer (section at end with hbox5).
I’ve fiddled with various different combinations but can’t get it to work
for me.
I’ve attached my working code (‘properties.py’ – using absolute position
for the buttons) , what I would like to know is how can I achieve the same
thing using sizers instead of absolute positioning with the buttons?
See below…
Is using sizers for buttons the best way to position them?
Definitely. It’s the best way to position anything in wxPython. Sizers make it such that your layout will look good on other platforms and setups with other resolutions than yours, allow for things to dynamically expand as you resize the frame (very important for look and feel) and also makes it easy for things to look lined-up and neat
To understand box sizers, think in terms of the vertical ones managing a vertical stack of things and the horizontal ones managing a horizontal row of things. Then realize that the “things” can themselves be sizers. So often a pattern one might use is a vertical boxsizer laying out a stack of rows, with each row being managed by a horizontal box sizer. See attached image. (With other types of sizers, you can also do something like this).
I’ve changed your code to have it do what you were trying to do. Basically, the mistake you made begins at line 35:
hbox2.Add(panel3,0)
You add panel3 to a horizontal boxSizer, as if you were going to make a “row” of visible items, starting with panel3 as the first thing in that row. But that’s not what you want. You want panel3 to be shown under the two large areas at top, and you want the buttons to form a row within panel3. So, panel3 has to be positioned with a vertical box sizer that will just add it as the lone item in the final row (under the two large areas). So, add it to the vbox sizer:
vbox.Add(panel3,0,flag=wx.ALIGN_CENTER)
Then, the buttons need to be in some horizontal box sizer that is associated with panel3. So the two steps are: 1) Associate a horizontal box sizer to panel3:
panel3.SetSizer(hbox2)
and then 2) Add the buttons to this hbox2 sizer:
hbox2.Add(button1,0)
hbox2.Add(button2,0, wx.LEFT | wx.BOTTOM , 5)
I think this gets it to about where you were aiming. I also moved SetSizerAndFit() to the end, since I thought that it may need to be called after all your laying-out is done so that it knows how much it has to fit to. This is a start, but you’ll see that you can make better layouts than this (just try removing the 20 point borders, for example).
There are a number of tutorials out there on wxPython sizers that you can get to by Googling. Sizers are tricky to think in terms of at first. Stay with it, and it will pay off.
Che
propertiesEdited.py (2.07 KB)
···
On Wed, Jul 14, 2010 at 10:00 PM, Seb S sebas.home1@gmail.com wrote: