Akash Sahu
Akash Sahu

Reputation: 11

Editing Specific column in a wx.ListCtrl

I have defined a wx.ListCtrl.

class FileList(wxw.ListCtrl, CheckListCtrlMixin, ListCtrlAutoWidthMixin): 
 style = wx.LC_REPORT | wx.BORDER_NONE | wx.LC_SORT_ASCENDING | \
 wx.LC_VRULES | wx.LC_HRULES | wx.LC_SINGLE_SEL | wx.LC_EDIT_LABELS 
 sizerProportion = 1
 sizerFlag = wx.EXPAND):
     pass

I want a specific column to be editable (3rd column of the row). Is there a way to do that? Any help will be highly appreciated.

Thanks

Upvotes: 1

Views: 2075

Answers (2)

yam ka
yam ka

Reputation: 27

Try this one

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)


    def OpenEditor(self, col, row):                     
                              
        if col == 0 : # make first column Editable
            self._editing = (col, row)
            listmix.TextEditMixin.OpenEditor(self, col, row)
########################################################################
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.InsertStringItem(index, row[0])
            self.list_ctrl.SetStringItem(index, 1, row[1])
            self.list_ctrl.SetStringItem(index, 2, row[2])
            self.list_ctrl.SetStringItem(index, 3, row[3])
            index += 1

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)
        self.SetSizer(sizer)


########################################################################
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

Jake
Jake

Reputation: 2625

You could just use the TextEditMixin to edit every "cell": http://www.wxpython.org/docs/api/wx.lib.mixins.listctrl.TextEditMixin-class.html

Then if you want to restrict which columns can be edited, define the OpenEditor handler and do an event.Veto() if you don't want that column/row to be editable.

Upvotes: 4

Related Questions