ObjectListView
An ObjectListView provides a more convenient and powerful interface to a wx.ListCtrl. The major features of an ObjectListView are:
- Automatically sorts rows by their data type.
Easily edits the values shown in the ListCtrl.
- Columns can be fixed-width, have a minimum and/or maximum width, or be space-filling.
- Displays a "list is empty" message when the list is empty (obviously).
- Supports custom formatting of rows
The FastObjectListView version can build a list of 10,000 objects in less than 0.1 seconds.
The VirtualObjectListView version supports millions of rows through ListCtrl's virtual mode.
- Supports alternate rows background colors.
An ObjectListView works in a declarative manner: the programmer configures how it should work, then gives it the list of objects to display. The primary configuration is in the definitions of the columns. Columns are configured to know which aspect of their model they should display, how it should be formatted, and even how new values should be written back into the model.
See the ObjectListView home page for more information. This site has lots of information about how to use this control, as well as a recipes, faq, and getting started information.
How do I use it?
1 from ObjectListView import ObjectListView, ColumnDefn
2
3 simpleColumns = [
4 ("Title", "left", 160, valueGetter="title", imageGetter="trackIcon", minimumWidth=40, maximumWidth=200),
5 ("Artist", valueGetter="artist", minimumWidth=40, maximumWidth=200, autoCompleteCellEditor=True),
6 ("Album", valueGetter="album", minimumWidth=50, autoCompleteCellEditor=True),
7 ("Genre", "left", 60, valueGetter="genre", autoCompleteComboBoxCellEditor=True),
8 ("Rating", "center", valueGetter="rating"),
9 ("Duration", "center", valueGetter="duration", stringConverter="%S seconds and %M minutes"),
10 ("Last Played", valueGetter="lastPlayed", stringConverter="%x %X", maximumWidth=100),
11 ("Comments", valueGetter="comments", minimumWidth=50, isSpaceFilling=True),
12 ]
13 self.olvSimple.SetColumns(simpleColumns)
14 self.olvSimple.SetObjects(self.dataObjects)
15 self.olvSimple.cellEditMode = ObjectListView.CELLEDIT_F2ONLY
The SetColumns() method defines what columns the control will have. The SetObjects() method tell the control which model objects the control will display. The control then takes those model objects and using the configuration information in the columns builds the contents of the list view.
With the above lines of code, the ObjectListView will
- automatically sort the rows when the header is clicked (and sort according to datatype)
- allows editing of the cells when the user presses F2. The cell editors used are chosen according to data type. The editors for the "Artist", "Album" and "Genre" will autocomplete from existing values.
- limit the minimum and maximum width of columns
resize the "Comments" column so that it fills any unused space on the list view (it become wider when the ListView is wider and narrower when the ListView is narrower)
Not bad for 3 lines of your code.
You can download the project, which includes the required python module and documentation, from here (version 1.2, released 6 September, 2008)