newbie question on auto sizing

I'm trying to create a floating toolbar with buttons that I want to be autosized to the minimum to
hold the buttons. I'm flailing.

···

#-----------------------------------------------------------------------------------
import wx

class Pallet(wx.Frame):
  def __init__(self, *args, **kwargs):
    super(Pallet,self).__init__(*args, **kwargs)
    self.panel = wx.Panel(self, wx.ID_ANY)
    self.panel.SetSizer(wx.BoxSizer(wx.HORIZONTAL))
    self.createButtons()
    self.Show()
    
  def buttonLabels(self):
    return ("Button1", "Button2", "Button3")
    
  def createButtons(self):
    labels= self.buttonLabels()
    buttonSize=(70,30)
    sizer=self.panel.GetSizer()
    for label in labels:
      button = wx.Button(self.panel, wx.ID_ANY, label, size=buttonSize)
      sizer.Add(button,0,wx.ALIGN_CENTER)
      
class MyApp(wx.App):
  def OnInit(self):
    pallet = Pallet(None,wx.ID_ANY,style=wx.FRAME_TOOL_WINDOW)
    pallet.Show()
    
if __name__ == '__main__':
  app = MyApp(0)
  app.MainLoop()
##-----------------------------------------------------------------------------------

What do I need to add to get the frame to shrink to the minimum size to hold the panel, holding the sizer, holding the buttons?
The code above is a total flail. The panel isn't maximizing size, The frame isn't shrinking...

If I skip the wx.FRAME_TOOL_WINDOW style, then just the sizing is wrong. The panel size problem goes away.

TIA,
Danny

NOTE:

In general, it's better to enclose code, rather than inlining it -- mailers tend to mangle whitespace.

I've enclosed a version that works. I changed a bunch of minor stuff (much of it for style, really), but the big change is that it creates the sizer, then add the buttons, then calls panel.SetSizerAndFit(Sizer).

You could probably have done it the way you originally had it, if you had called self.panel.Fit() after adding the buttons.

Note that I got rid of your explicit size for the buttons -- that's what sizers are for!

-Chris

ButtonTest.py (953 Bytes)

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Christopher Barker wrote:

the big change is that it creates
the sizer, then add the buttons, then calls panel.SetSizerAndFit(Sizer).

You could probably have done it the way you originally had it, if you had called self.panel.Fit() after adding the buttons.

Oh, and self.Fit() too, to fit the Frame to the Panel.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov