How can I edit a label

Editing an entry in the list is simple, except in report lists, where the user can only edit the first column in each row. For other kinds of lists, that's not an issue; the regular label for each item is editable.

To make a list editable, include the style flag wx.lc_edit_labels in the constructor when the list is created.

list = wx.ListCtrl(self, -1, style=wx.LC_REPORT | wx.LC_EDIT_LABELS)

If the edit flag is set, the user can start an edit session by clicking on a list item that has already been selected (it doesn't have to be a quick double-click). The edit box is similar to editing within Windows Explorer. The user receives a small edit-in-place box where the text label was. Pressing Enter ends the edit session, and the new text becomes the text label. A mouse click elsewhere in the list control also ends the edit session (there can only be one edit session at a time). Pressing Escape cancels the edit session, and the new text is discarded.

The following two event types are triggered by an editing session.



Remember that if you want the normal processing to proceed in addition to your custom handler, you need to include a call to the event method Skip() in your event handler. A list event with the event type evt_list_begin_label_edit is triggered when the user starts an edit session, and one with the event type EVT_LIST_END_LABEL_EDIT is triggered when the session ends (either by an Enter or an Escape cancel). You can veto the begin edit event, in which case the edit session does not start. Vetoing the end edit event prevents the list text from changing.

The wx.ListEvent class has a couple of properties that are only interesting when processing an EVT_LIST_END_LABEL_EDIT event. The method GetLabel() returns the new text of the list item label after the edit has been completed and okayed. If the edit was canceled via the escape key, then the method returns an empty string. This means that you cannot use GetLabel() to distinguish between a cancel and the user deliberately changing the item text to an empty string. If required, the method lsEditCancelled() returns True if the edit end was due to a cancel, and False otherwise. Use that method when you need to distinguish between a successful edit session and a canceled one.

If you want to have some other user event start an edit session, you can trigger an edit programmatically using the list control method EditLabel(item), where the item parameter is the index of the list item to be edited. This method triggers the evt_list_begin_label_edit event, and the edit session continues just as though it had been initiated in the usual way.

If you would like to directly manipulate the text control being used for the editing, you can get it with the list control method GetEditControl(). The method returns the text control being used for the current edit. If there is no current edit, the method returns None. Currently, the method only works under Windows operating systems.

Was this article helpful?

+1 0

Post a comment