Minimum size for wx.ListCtrl?

I've been using wxPython for a few years and can generally figure
things out, but I'm really stumped by this one. I'm trying to use a
wx.ListCtrl for this first time, but the default sizing isn't what I
would like.

It appears that for a wxListCtrl the following
  GetSize()
  GetBestSize()
  GetEffectiveMinSize()
always return (100, 80) regardless of the contents of the wxListCtrl.

I would like to set the size of the wxListCtrl so that it is big
enough to not need scroll bars. Is there an easy way to do this?

If it helps, I have an example below.

Jeff

#!/usr/bin/env python

import wx
import os
import string
import sys
import wx.lib.mixins.listctrl as listmix

···

##################################################################

class MyFrame(wx.Frame):

  def __init__(self, parent):
    wx.Frame.__init__(self, parent, -1, "Test")

    panel = BallotsPanel(self)

    sizer = wx.BoxSizer()
    sizer.Add(panel, 1, wx.EXPAND, 0)
    self.SetSizer(sizer)
    sizer.Fit(self)

##################################################################

class BallotCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):

  def __init__(self, parent, ID, pos=wx.DefaultPosition,
               size=wx.DefaultSize):
    style=wx.LC_REPORT|wx.BORDER_NONE
    wx.ListCtrl.__init__(self, parent, ID, pos, size, style)
    listmix.ListCtrlAutoWidthMixin.__init__(self)

##################################################################

class BallotsPanel(wx.Panel):

  def __init__(self, parent):
    wx.Panel.__init__(self, parent, -1)

    names = ["Condit, James", "Davis, Henrietta", "Decker, Marjorie",
"Dixon, Vince", "Gallucio, Anthony", "Hall, Robert", "Horowitz,
Jacob"]

    self.ballotC = BallotCtrl(self)
    self.ballotC.InsertColumn(0, "R", wx.LIST_FORMAT_RIGHT)
    self.ballotC.InsertColumn(1, "Candidate")
    for c, name in enumerate(names):
      self.ballotC.InsertStringItem(c, "")
      self.ballotC.SetStringItem(c, 1, name)
    self.ballotC.SetColumnWidth(0, wx.LIST_AUTOSIZE_USEHEADER)
    self.ballotC.SetColumnWidth(1, wx.LIST_AUTOSIZE_USEHEADER)
    print self.ballotC.GetSize()
    print self.ballotC.GetBestSize()
    print self.ballotC.GetClientSize()
    print self.ballotC.GetVirtualSize()
    print self.ballotC.GetEffectiveMinSize()

    # Sizers
    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(self.ballotC, 1, wx.EXPAND)
    self.SetSizer(sizer)
    sizer.Fit(self)

##################################################################

class App(wx.App):
  def OnInit(self):
    self.frame = MyFrame(None)
    self.frame.Show(True)
    self.frame.Center()
    self.SetTopWindow(self.frame)
    return True

##################################################################

if __name__ == '__main__':
  app = App(0)
  app.MainLoop()

Hi Jeffrey,

I've been using wxPython for a few years and can generally figure
things out, but I'm really stumped by this one. I'm trying to use a
wx.ListCtrl for this first time, but the default sizing isn't what I
would like.

It appears that for a wxListCtrl the following
  GetSize()
  GetBestSize()
  GetEffectiveMinSize()
always return (100, 80) regardless of the contents of the wxListCtrl.

I would like to set the size of the wxListCtrl so that it is big
enough to not need scroll bars. Is there an easy way to do this?

If it helps, I have an example below.

Jeff
  
It looks like the "sizer.Fit(self)" line in your MyFrame class is the issue. I commented that line out on my machine and it worked fine then. I'm using Windows XP, wxPython 2.8.9.1., Python 2.5.2. I can't tell you exactly why this is happening though. I'm guessing that when the Fit is called in the Frame, it's doing it before the ListCtrl is fully initialized so that the panel is not expanded yet. You could try sticking the Fit() call in MyFrame in a wx.CallAfter() to see if that works...

Or maybe someone else will have a clue.

···

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org

Jeffrey O'Neill wrote:

I've been using wxPython for a few years and can generally figure
things out, but I'm really stumped by this one. I'm trying to use a
wx.ListCtrl for this first time, but the default sizing isn't what I
would like.

It appears that for a wxListCtrl the following
  GetSize()
  GetBestSize()
  GetEffectiveMinSize()
always return (100, 80) regardless of the contents of the wxListCtrl.

I would like to set the size of the wxListCtrl so that it is big
enough to not need scroll bars. Is there an easy way to do this?

The wx.ListCtrl doesn't have a DoGetBestSize method that uses the content to calculate the size. This is likely because in typical usage it will have more items than can be shown without scrollbars. If the best size was calculated based on the content then most listctrl's in a sizer would cause them to blow up in size. If you want to calculate the size yourself you can set the minsize and then the sizer will honor that.

You can also look at the ListCtrlAutoWidthMixin class in wx.lib.mixins.listctrl for an example of calculating and autosizing the width.

···

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