Here's a mixin for ListCtrl controls in LC_REPORT mode to help set the
initial size (and resize on list size events) of all columns. The
idea is to provide a reasonable default size for all columns without
specifying everything in fixed pixel widths. It will attempt to use
all the constraints in order to show as many columns as possible
without using a horizontal scroll bar. ColumnSizer works well with
the wx.lib.mixins.listctrl mixins.
Column sizes can have a minimum or maximum value (or can be fixed
width). Sizes can be specified in pixels or given as a representative
string that the method will turn into a pixel width based on the
current font. Columns can also be told to be greedy when resizing, or
can be allowed to be off screen.
Source available (wxPython license) here:
http://trac.flipturn.org/browser/trunk/peppy/lib/columnsizer.py
Run the built-in demo with 'python columnsizer.py'
From the main doc string:
"""columnsizer -- a mixin to handle column resizing for list controls
This mixin provides the capability to resize columns in a report-mode list
control. Columns may have fixed or scalable widths, and optionally an initial
group of columns can be constrained to fit in the visible portion of the
window upon resizing or initialization of the list.
Usage
···
=====
This mixin is used in subclasses of ListCtrl in LC_REPORT mode, as in the
following snippet from a constructor:
class TestList(ColumnSizerMixin, wx.ListCtrl):
def __init__(self, parent):
wx.ListCtrl.__init__(self, parent, style=wx.LC_REPORT)
ColumnSizerMixin.__init__(self)
Columns are added using the special InsertSizedColumn method, not the standard
InsertColumn method of the ListCtrl. InsertSizedColumn has several keyword
arguments that control how the column is resized; see the docstring for the
method for a full description. For example,
def createColumns(self):
self.InsertSizedColumn(0, "Index")
self.InsertSizedColumn(1, "Filename", min=100, max=200)
self.InsertSizedColumn(2, "Size", wx.LIST_FORMAT_RIGHT,
min=30, scale=False)
self.InsertSizedColumn(3, "Mode", max="MMM")
self.InsertSizedColumn(4, "Description", min=100)
self.InsertSizedColumn(5, "URL", ok_offscreen=True)
self.InsertSizedColumn(6, "Owner", fixed=50)
will create 6 columns, all autosized to the largest item but with the
additional constraints applied in the following cases:
* Column 1 (Filename) will be clamped to the range between minimum and maximum
pixels.
* Column 2 (Size) will be aligned to the right, has a minimum size, and won't
be greedy about its maximum size when the whole list is scaled.
* Column 3 (Mode) has a maximum allowable size given by the width in pixels of
the string "MMM" in the ListCtrl's current font.
* Column 4 (Description) has a minimum size is pixels
* Column 5 (URL) is allowed to be initially placed offscreen, as it is expected
to be a very wide column. Note that by allowing this column to be placed
offscreen, subsequent columns would also be allowed offscreen. Only columns
before an ok_offscreen=True keyword argument are scaled in an attempt to keep
those on screen.
* Column 6 (Owner) is a fixed width of 50 pixels
Once the columns are set up and the list is populated with data, and every time
you add more data and want the column sizes to be recomputed, you must make a
call to ResizeColumns. This will recalculate and redisplay the columns.
Bugs and to-do items
* the column sizes after a user resize (i.e. the user uses the mouse, grabs
a border between columns, and resizes a column) are not saved. ResizeColumn
ignores the user changes and resizes columns based on how they were set up
in InsertSizedColumn.
"""