wx.ListBox and wx.GridBagSizer resize issue

I have a wx.ListBox inside of a wx.GridBagSizer. When I set text in the
wx.ListBox that is too wide, I want a horizontal scroll bar to appear. However,
when I resize the frame, the wx.ListBox grows to the width of the text, the
scroll bar disappears, and the list box no longer expands with the sizer. What
am I doing wrong? Do I need to bind to a wx.EVT_SIZE event somehow? Below is a
minimalist example of my problem. Click the button and then resize the frame to
see the resizing problem.

import wx

class GUI(wx.App):
    def OnInit(self):
        self.frame = MainFrame(None,size=(300,200))
        self.frame.Show()
        return True

class MainFrame(wx.Frame):
    def __init__(self,*args,**kwargs):
        wx.Frame.__init__(self,*args,**kwargs)

        self.panel = MyPanel(self)

        mainSizer = wx.BoxSizer(wx.VERTICAL)
        mainSizer.Add(self.panel,1,wx.ALL|wx.EXPAND,border=10)
        self.SetSizer(mainSizer)
        mainSizer.Layout()

class MyPanel(wx.Panel):
    def __init__(self,parent):
        wx.Panel.__init__(self,parent,wx.ID_ANY)

        bagSizer = wx.GridBagSizer(hgap=5,vgap=5)

        self.myButton = wx.Button(self,wx.ID_ANY,"Push Me")
        self.myListBox = wx.ListBox(self,wx.ID_ANY,style=wx.LB_HSCROLL)

        bagSizer.Add(self.myButton,pos=(0,0),flag=wx.ALL,border=5)
       
bagSizer.Add(self.myListBox,pos=(0,1),span=(2,1),flag=wx.ALL|wx.EXPAND,border=5)

        bagSizer.AddGrowableCol(1)
        bagSizer.AddGrowableRow(1)

        self.SetSizer(bagSizer)

        self.Bind(wx.EVT_BUTTON,self.OnButton,self.myButton)

    def OnButton(self,event):
        self.myListBox.Set(["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"])

if __name__ == "__main__":

    app = GUI()
    app.MainLoop()

That is because the listbox's best size is to be wide enough to show the longest item, and sizers will try to honor that best size. You can override that by setting the widget's minsize. Then in your case the listbox will always be able to be sized down to that size regardless of what the best size is.

···

On 5/12/12 9:46 PM, James Henderson wrote:

I have a wx.ListBox inside of a wx.GridBagSizer. When I set text in the
wx.ListBox that is too wide, I want a horizontal scroll bar to appear. However,
when I resize the frame, the wx.ListBox grows to the width of the text, the
scroll bar disappears, and the list box no longer expands with the sizer. What
am I doing wrong? Do I need to bind to a wx.EVT_SIZE event somehow? Below is a
minimalist example of my problem. Click the button and then resize the frame to
see the resizing problem.

--
Robin Dunn
Software Craftsman