ListCtrl with textedit mixin

Essentially every problem I am having seems to boil down to the same problem. I can’t find the information or examples I need to do things.

The latest case is wx.ListCtrl. I simply want to show a list of records (rows and columns) and allow the user to edit fields. I can display the records and I can edit the fields but I don’t know how to get feedback on what was edited. The examples I have been able to find are either

  1. For much older versions of wxPython/Python that no longer run
  2. Include so many extra features that the simple example is obscured beyond recognition
  3. Do not go far enough (ie allow editing but no feedback)
  4. Omit details necessary to create fully functioning code

For example, I want to provide a handler for the textedit mixin so that I can do processing on begin and end edit. On the begin edit I could save the values of event.m_row and event.m_col so that on end edit I could update any internal data structures. The examples I have seen say this can be done in OnEditBegin and OnEditEnd but do not say what events to use to connect those handlers. One example says to use wx.EVT_LIST_BEGIN_LABEL_EDIT, but this results only in

AttributeError: 'ListEvent' object has no attribute 'm_row'

I saw one suggestion from Robin that I should be using ObjectListView. I tried this, but first had to spend several hours patching the code just to get the examples working (they have not been updated since 2008). When I tried to add editing I found the documentation similarly lacking in sufficient details. This is unfortunate because I especially liked the GroupListView capability. As is typical in wxPython it seems that I need to be proficient enough to not need the documentation in order for the documentation to make sense. A definite catch-22.

But I digress.

How can I connect begin-edit and end-edit handlers so that I can add custom code to identify the row and column being edited?

Does event.Index and event.Column do what you want?

import wx
from wx.lib.mixins.listctrl import TextEditMixin

DATA = (
    ("001", "Fred"),
    ("002", "Joe"),
    ("003", "Harry")
)

class EditListCtrl(wx.ListCtrl, TextEditMixin):

    def __init__(self, parent):
        wx.ListCtrl.__init__(self, parent, style=wx.LC_REPORT)
        TextEditMixin.__init__(self)

        self.InsertColumn(0, "ID")
        self.InsertColumn(1, "Name")

        for entry in DATA:
            self.Append(entry)

        self.Bind(wx.EVT_LIST_BEGIN_LABEL_EDIT, self.OnEditBegin)
        self.Bind(wx.EVT_LIST_END_LABEL_EDIT,   self.OnEditEnd)

    def OnEditBegin(self, event):
        print("Begin: Row=%d Column=%d" % (event.Index, event.Column))

    def OnEditEnd(self, event):
        print("End  : Row=%d Column=%d Text='%s'" % (event.Index, event.Column, event.Item.Text))


class ListFrame(wx.Frame):

    def __init__(self, parent):
        wx.Frame.__init__(self, parent)

        self.lc = EditListCtrl(self)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.lc, 1, wx.EXPAND, 0)


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

if __name__ == "__main__":
    app = MyApp(0)
    app.MainLoop()

Hello,

The python language is great but it is evolving rapidly. It is good and bad at the same time.
The code quickly becomes obsolete if it is not updated regularly.
Many very interesting libraries or many examples or samples are also not updated.
For me it’s a waste and a huge restoration job for people who want to use them.
It’s only my opinion!

-------------

There are very few simple examples on how to add, modify, delete data in a standard or virtual list control and how to save them in a file (text, csv…) or an SQLite database or other.

For list control you can try the links below:

Mike Driscoll’s blog:

or:

wxPython demo:

-Index of /wxPython4/extras/4.2.0
-Index of /Phoenix/snapshot-builds

or again Mike and Cody’s books.

Good luck!

(thanks to the online translator)

That’s what I want but I’d be more interested in how or where I could find this out myself instead of constantly having to ask these types of questions here. According to the wxPython official docs this info is not on the textedit mixins page. When I search for wx.EVT_LIST_BEGIN_LABEL_EDIT I just get

Your search did not match any documents

Under wx.ListCtrl I find (selecting Events Emitted…)

EVT_LIST_BEGIN_LABEL_EDIT: Begin editing a label. 

but I am not editing a label so why should I assume this would be the event I want. Also, under Properties for Column i see

Column         See GetColumn and SetColumn

and under GetColumn I see

GetColumn(self, col)
    Gets information about this column. See SetItem() for more information.

Return type
    wx.ListItem

and looking for what I can retrieve from the event parameter under Wx.ListEvent I see that Column

The column position: it is only used with COL events.

So round and round and round. I hope you can understand my frustration with this. I know that what information I do get is the result of many hours of work by unpaid volunteers and I appreciate the effort.

zig_zag

Once again you have been very helpful. The first place I went was to the two books by Cory Precord and Mike Driscoll, and I have seen the examples on the blog and wxdemo. Both had stripped down examples of the textedit mixin that showed just enough to illustrate that cell editing is possible, but not enough to make actual use of the mixin.

I forgot to tell you about the old but excellent book that follows :
WxPython in Action (Dunn and Rapin)

Thanks. That was the first book I got when I started using wxPython a few years ago.

When I first started with wxPython I was a complete novice with not only with wxPython but also Python. I started with Python 2.x. And if you think the Doc’s are not helpful you should have seen it back then. But what actually saved me was my editor. I happen to use Wing IDE. I was able to use the debugger to determine what was available - such as the events you noted. I just continued to play with all the debugger had to offer until I found a solution to what every problem I was trying to solve. I still do it today when I run into some issue. I have come to the conclusion that the Doc’s (along with demo code) just help with a starting point. What works for me has been the debugger. As I said I use Wing but there are others I have used VS code and pyCharm and their debuggers have been very helpful. BTW I do NOT consider myself any sort of expert in anyway. There is much I do NOT understand when it comes to wxPyhthon.
Johnf

I used to use Visual Studio to debug my python code (this was back when I was still writing vb.Net code). When I got my new laptop last month I decided to skip Visual Studio since everything I was writing was in python and I hadn’t done anything in VB for almost two years. Plus VS is a bear for disk and memory.

I’ve tried PyCharm but I’m just not comfortable with it. There are a lot of features I do not yet know how to use (or if they even exist). I haven’t decided yet if I’ll go back to VS, or juust keep wrestling with PyCharm until I am more fluent.

I don’t want to be awkward and discussing documentation will rightly be an endless topic. Commercially it’s a money maker (consultation etc), i.e a real product just as the software itself. This wxPython is, as far as I can judge, a very well-maintained project combining a professional Gui library with a leading scripting language and an excellant discussion forum, all for free.
If you run a commercial software and want the service you demand you’ll need very deep pockets and would be chuffed of the conditions here (hacking included) :pleading_face:

for a scripting language a debugger should not be decisive: you have the print & the f-string, which should go a long way; for a no-fuss python editor with debugging I use PyScriptor for tracing, editing of course in Geany (GTK+ Gui) :relaxed:

Print and f string cannot compare to the power of setting a breakpoint and having intellisense available to see what is exposed by typing “event.” in the shell and seeing what pops up. Or adding a watch and expanding the target.

1 Like

if you are that far off-topic PyScriptor would be your first choice, Geany goes a more lean way and tries to support all sorts of languages so that one need not change the editor for language change (last discussion was about a poor live view support for HTML, after all it’s also free) :star_struck:

@Zig_Zag where do you see the rapid evolvement for the average user? unless he is still using 2.x :thinking:

The first thing PyScriptor did when I installed it was to complain that it could not find a compatible version of Python (>= 3.6). I’m running 3.10.5 from python.org (Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32), the site it said to get the latest version from. When I tried to add it manually it complained again. I think I’ll stick with PyCharm.

well, I don’t work in the sales department of JetBrains (although I had to go to Czechoslovakia after the wall came down for the company I then worked for) nor for the PyScripter project (I highly admire their efforts & I was surprised what one can do in Pascal!) and anybody should choose their tools as they think fits best & should read the docs: quite often they are pretty useful (ok, after the pressure has subsided: we all work somewhat similar) :ghost: