A changing number of buttons inside a sizer

Hi everyone,

This is about having a changing number of button objects placed inside
a sizer.

The buttons have the following properties:

1. They are all of the same size, and will have some text and an icon
on them.
2. The number of buttons will change dynamically while the application
runs.
3. The text and icons on the buttons will also change dynamically.

What i would like to do automatically, after the number of buttons
changed, is:

4. Choose the button size such that the set of all buttons fits the
visible part of the screen.
5. Choose the number of columns and rows of the sizer such that the
set of all buttons fits the screen.
6. Choose a single font such that all the text labels of the buttons
fit their button, and are readable.

Currently i am using GenBitmapTextButton as buttons and GridSizer as
sizer. But i am doing some things myself that i presume wxPython will
gladly do *for* me :slight_smile:

Like setting the number of columns/rows of the GridSizer to make the
set of buttons fit the screen. Or like setting all buttons to the
largest of all best-sizes of all buttons to force every button being
the same size.

My question(s):
Is there a better way than using GenBitmapTextButtons inside
GridSizers for a changing set of buttons?
Where should i draw the line between what i let wxPython do *for* me
and what i do myself in the code?

And any other thought you might have on this subject is welcome.

Best regards,
Maarten Stol

Like setting the number of columns/rows of the GridSizer to make the
set of buttons fit the screen.

You'll need to create a new sizer with the new dimensions and then add the buttons to that sizer. Something like this

  newSizer = wx.GridSizer(rows, cols)
  for item in oldSizer.GetChildren():
    oldSizer.Detach(item.GetWindow())
    newSizer.Add(item.GetWindow())
  self.SetSizer(newSizer)

Or like setting all buttons to the
largest of all best-sizes of all buttons to force every button being
the same size.

wx.GridSizer already does that for you. It makes the size of each cell the size needed by the largest of the items. If you add the items with the wx.EXPAND flag then they will each fill their cell.

···

On 11/5/09 8:26 AM, maarten.stol wrote:

--
Robin Dunn
Software Craftsman

Thank you Robin,

The buttons are all the same size now, and giving the sizer other
dimensions also works.

However, I would still like to have the set of buttons automatically
fill the available screen space.
I have been reading about sizers in the online docs, but could not
easily find a solution for this problem.

Is there a way to give the sizer its dimensions given how much screen
space is available for the sizer?

regards,
Maarten

···

On Nov 6, 2:45 am, Robin Dunn <ro...@alldunn.com> wrote:

On 11/5/09 8:26 AM, maarten.stol wrote:

> Like setting the number of columns/rows of the GridSizer to make the
> set of buttons fit the screen.

You'll need to create a new sizer with the new dimensions and then add
the buttons to that sizer. Something like this

    newSizer = wx\.GridSizer\(rows, cols\)
    for item in oldSizer\.GetChildren\(\):
            oldSizer\.Detach\(item\.GetWindow\(\)\)
            newSizer\.Add\(item\.GetWindow\(\)\)
    self\.SetSizer\(newSizer\)

> Or like setting all buttons to the
> largest of all best-sizes of all buttons to force every button being
> the same size.

wx.GridSizer already does that for you. It makes the size of each cell
the size needed by the largest of the items. If you add the items with
the wx.EXPAND flag then they will each fill their cell.

--
Robin Dunn
Software Craftsmanhttp://wxPython.org

Just make the size of the window larger (or use Maximize()) and the sizer will use all the space given to it by the owning window, or the owning sizer if it is a nested sizer.

···

On 11/6/09 8:37 AM, maarten.stol wrote:

Thank you Robin,

The buttons are all the same size now, and giving the sizer other
dimensions also works.

However, I would still like to have the set of buttons automatically
fill the available screen space.
I have been reading about sizers in the online docs, but could not
easily find a solution for this problem.

Is there a way to give the sizer its dimensions given how much screen
space is available for the sizer?

--
Robin Dunn
Software Craftsman