Because this question comes up periodically, I have attached a mixin that
enables any text in any column of a multi-column listctrl to be edited by
clicking on the given row and column. You close the text editor by hitting
the ENTER key or clicking somewhere else on the listctrl.
Note that I am using 2.4.x but this should work with 2.5.x. Also, this has
only been tested on Win XP, Python 2.3.3; I have no idea if it works on
*nix.
To enable the mixin to you add the following lines to the demo's
wxListCtrl.py:
1) Assuming you put the mixin in wxPython.lib.mixins then you need to add
this import
from wxPython.lib.mixins.wxListCtrlTextEdit import TextEdit
2) The Class definition for TestListCtrl is changed as follows
# add TextEdit mixin to class TestListCtrl
class TestListCtrl(wxListCtrl, wxListCtrlAutoWidthMixin, TextEdit):
def __init__(self, parent, ID, pos=wxDefaultPosition,
size=wxDefaultSize, style=0):
wxListCtrl.__init__(self, parent, ID, pos, size, style)
wxListCtrlAutoWidthMixin.__init__(self)
# call TextEdit __init__
TextEdit.__init__(self)
3) In the method OnItemSelected in class TestListCtrlPanel (line ~212) add
the line:
self.list.curRow = event.m_itemIndex
Note that the mixin requires that EVT_LIST_ITEM_SELECTED be bound to a
method that sets the listctrl's curRow attribute.
···
--------------------------------------------------
For text in columns of the ListCtrl that are left-justified the text editor
lines up pretty well with the text although you can always add some empiric
adjustment factors if you need them. In the demo, one column is right
justified so you could check for that and use a textctrl with style =
wxTE_RIGHT for right-justified columns but I didn't take the time to do
that.
And finally, if the edited text needs to be saved somewhere permanently
(like to a db) then you would need to take add the code to take care of
that.
Steve