Rolf of Saxony
Rolf of Saxony

Reputation: 22443

Wxpython Phoenix editable listctrl unable to access the changed data

Prior to Phoenix an editable listctrl used to pass back the edited text via the event in event.Label.
Using wxpython 4.0.0b2 (Linux) this is no longer appears to be the case.
Looking at the code, the text should be passed back in event.Item.Text but it is always blank.

The only way that I have found of by-passing this is to edit the "listctrl.py" mixin itself, adding an extra line of code to the CloseEditor function, where I set event.String to return the altered text and then access that when the wx.EVT_LIST_END_LABEL_EDIT binding is triggered.
It would have been better to set event.Label but the SetLabel function seems to have been lost along the way.

I feel sure that I must have missed something somewhere.
In short, does anyone know how to access the label that has been altered in the TextEditMixin in wxpython Phoenix?
What I am struggling with appears to be an EVT_LIST_END_LABEL_EDIT event with no access to the label itself.
Example code:

import wx
import wx.lib.mixins.listctrl as listmix

class EditableListCtrl(wx.ListCtrl, listmix.TextEditMixin):
    ''' TextEditMixin allows any column to be edited. '''

    def __init__(self, parent, ID=wx.ID_ANY, pos=wx.DefaultPosition,
                 size=wx.DefaultSize, style=0):
        """Constructor"""
        wx.ListCtrl.__init__(self, parent, ID, pos, size, style)
        listmix.TextEditMixin.__init__(self)

class MyPanel(wx.Panel):

    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)
        rows = [("Ford", "Taurus", "1996", "Blue"),
                ("Nissan", "370Z", "2010", "Green"),
                ("Porche", "911", "2009", "Red")
                ]
        self.list_ctrl = EditableListCtrl(self, style=wx.LC_REPORT)
        self.list_ctrl.InsertColumn(0, "Make")
        self.list_ctrl.InsertColumn(1, "Model")
        self.list_ctrl.InsertColumn(2, "Year")
        self.list_ctrl.InsertColumn(3, "Color")
        index = 0
        for row in rows:
            self.list_ctrl.InsertItem(index, row[0])
            self.list_ctrl.SetItem(index, 1, row[1])
            self.list_ctrl.SetItem(index, 2, row[2])
            self.list_ctrl.SetItem(index, 3, row[3])
            index += 1
        self.text0 = wx.TextCtrl(self, -1, "Ford", size=(50,30))
        self.text1 = wx.TextCtrl(self, -1, "Taurus", size=(50,30))
        self.text2 = wx.TextCtrl(self, -1, "1996", size=(50,30))
        self.text3 = wx.TextCtrl(self, -1, "Blue", size=(50,30))
        self.list_ctrl.Select(0)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer2 = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)
        sizer2.Add(self.text0, 0, wx.ALL|wx.EXPAND, 5)
        sizer2.Add(self.text1, 0, wx.ALL|wx.EXPAND, 5)
        sizer2.Add(self.text2, 0, wx.ALL|wx.EXPAND, 5)
        sizer2.Add(self.text3, 0, wx.ALL|wx.EXPAND, 5)
        sizer.Add(sizer2,0,wx.ALL|wx.EXPAND,5)
        self.SetSizer(sizer)
        self.list_ctrl.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
        self.list_ctrl.Bind(wx.EVT_LIST_END_LABEL_EDIT, self.OnMixUpdate)

    def OnItemSelected(self, event):
        self.currentItem = event.GetIndex()
        rowid = self.list_ctrl.GetItem(self.currentItem,0)
        self.SetData()
        event.Skip()

    def SetData(self):
        rowid = self.list_ctrl.GetItem(self.currentItem,0)
        self.text0.SetValue(rowid.GetText())
        rowid = self.list_ctrl.GetItem(self.currentItem,1)
        self.text1.SetValue(rowid.GetText())
        rowid = self.list_ctrl.GetItem(self.currentItem,2)
        self.text2.SetValue(rowid.GetText())
        rowid = self.list_ctrl.GetItem(self.currentItem,3)
        self.text3.SetValue(rowid.GetText())

    def OnMixUpdate(self, event):
        self.currentItem = event.GetIndex()
        rowid = event.GetIndex()
        new_data = event.GetLabel()
        new_data2 = event.Item.GetText()
        colid = event.GetColumn ()
        print ("row,col,new label:",rowid,colid,new_data)
        print ("2nd attempt:",new_data2)
        self.list_ctrl.SetItem(rowid,colid,new_data,)
        #Update the textctrl on screen
        self.SetData()
        #Update database
        #self.OnUpdate(None)
        event.Skip()

class MyFrame(wx.Frame):

    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, wx.ID_ANY, "Editable List Control")
        panel = MyPanel(self)
        self.Show()

if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()

Upvotes: 0

Views: 226

Answers (1)

Rolf of Saxony
Rolf of Saxony

Reputation: 22443

This turned out to be a bug and was fixed in wx.python 4.0.0

Upvotes: 0

Related Questions