Sizing a window to a list control

Hi,

I'm new to wxPython, and I want to get a window to size itself to the
contents of a wxListCtrl. Currently I have the following code:

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)
        lc = wx.ListCtrl(self, -1, style=wx.LC_REPORT)
        lc.InsertColumn(0, 'Heading 1')
        lc.InsertColumn(1, 'Heading 2')
        lc.InsertColumn(2, 'Heading 3')
        for i in range(10):
            lc.InsertStringItem(i, str(i))
            lc.SetStringItem(i, 1, 'spam')
            lc.SetStringItem(i, 2, 'eggs')
        lc.SetColumnWidth(0, wx.LIST_AUTOSIZE_USEHEADER)
        lc.SetColumnWidth(1, wx.LIST_AUTOSIZE_USEHEADER)
        lc.SetColumnWidth(2, wx.LIST_AUTOSIZE_USEHEADER)

app = wx.PySimpleApp()
frame = MyFrame(None, -1, 'Test')
frame.Show()
app.MainLoop()

When I run it, the list control displays fine, however there is a pile of
empty space below and to the right of the list. I want to size the
containing window so its bottom edge is 'just below' the last item on the
list and the right edge is 'just to the right' of the third column.

Could someone please point me towards what I need to do to achieve this?

Thanks in advance,

Simon

Simon Lieschke wrote:

Hi,

I'm new to wxPython, and I want to get a window to size itself to the
contents of a wxListCtrl. Currently I have the following code:

When I run it, the list control displays fine, however there is a pile of
empty space below and to the right of the list. I want to size the
containing window so its bottom edge is 'just below' the last item on the
list and the right edge is 'just to the right' of the third column.

Could someone please point me towards what I need to do to achieve this?

I thought this would be easy, but I couldn't get it to work. The problem
seems to be that a ListCtrl automatically uses scroll bars if it gets
too small to show anything, and it can thus be essebtially any size.
GetBestSize reports a very small size, so that doesn't work.
GetVirtualSize seems to report exactly the same size as it gets
displyaed by default, and that is wider than it needs to be.
Interestingly, on my system (Python2.3 wxPython 2.4.2.4 wxGTK), it
displays with a vertical scrollbar, rather than being taller than
required.

It seems to me that there is a bug/missing feature, if there really is
no way to find out how big the ListCtrl would be with no scrollbars and
no extra space. Have I missed a method?

Note: one thing I didn't try was putting it in a Sizer on the Frame...I
can't see how that would make a difference, but fit may be worth a
try...

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                        
NOAA/OR&R/HAZMAT (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

Simon Lieschke wrote:

Hi,

I'm new to wxPython, and I want to get a window to size itself to the
contents of a wxListCtrl. Currently I have the following code:

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)
        lc = wx.ListCtrl(self, -1, style=wx.LC_REPORT)
        lc.InsertColumn(0, 'Heading 1')
        lc.InsertColumn(1, 'Heading 2')
        lc.InsertColumn(2, 'Heading 3')
        for i in range(10):
            lc.InsertStringItem(i, str(i))
            lc.SetStringItem(i, 1, 'spam')
            lc.SetStringItem(i, 2, 'eggs')
        lc.SetColumnWidth(0, wx.LIST_AUTOSIZE_USEHEADER)
        lc.SetColumnWidth(1, wx.LIST_AUTOSIZE_USEHEADER)
        lc.SetColumnWidth(2, wx.LIST_AUTOSIZE_USEHEADER)

app = wx.PySimpleApp()
frame = MyFrame(None, -1, 'Test')
frame.Show()
app.MainLoop()

When I run it, the list control displays fine, however there is a pile of
empty space below and to the right of the list. I want to size the
containing window so its bottom edge is 'just below' the last item on the
list and the right edge is 'just to the right' of the third column.

Could someone please point me towards what I need to do to achieve this?

For wxListCtrl I don't think GetBestSize reutns a meaningful value, so
you have to work it our for yourself. You can add up all the col widths
for the total width, and then get the font height and multiply by your
number of rows+1 and you'll be pretty close on the height.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!