Search code examples
wxpython

Not able to get selected item in wx.DataViewCtrl


I am using wx.DataViewCtrl of wxpython 2.7.8 to list my data. I can get selected item from list using wx.DataViewCtrl:GetSelections() at first time by clicking menu. after closing the box by clicking close button, again am trying to open this box by clicking menu. But i coudn't get selected item to delete/modify using wx.DataViewCtrl:GetSelections(). wx.DataViewCtrl:GetSelections() returns empty list. But still the wx.DataViewCtrl box displaying the list of data.

Please help me. I am struck with this issue for last two days.Thanks in advance.

    class MainFrame(wx.Dialog):

def __init__(self, parent, title, data):

    self.data = data
    wx.Dialog.__init__(self, parent, title=title, size=(800, 400))
    self.panel = wx.Panel(self)       
    self.model = UpdateModel(self.data)



def createEntityTable(self, editor_ctrl):
    self.__editor_ctrl = editor_ctrl.getEditor()    
    self.dvc = dv.DataViewCtrl(self.panel,
                               style=wx.BORDER_THEME
                               | dv.DV_ROW_LINES # nice alternating bg colors
                               #| dv.DV_HORIZ_RULES
                               | dv.DV_VERT_RULES
                               | dv.DV_MULTIPLE, size = (600, 400)
                               )
    self.dvc.AssociateModel(self.model)

    self.dvc.AppendTextColumn("Name",  0, width=170, mode=dv.DATAVIEW_CELL_EDITABLE)
    self.dvc.AppendTextColumn("DTD",   1, width=80, mode=dv.DATAVIEW_CELL_EDITABLE)
    self.dvc.AppendTextColumn("File name",   2, width=80,  mode=dv.DATAVIEW_CELL_EDITABLE)
    self.dvc.AppendTextColumn("Public Id",   3, width=80,  mode=dv.DATAVIEW_CELL_EDITABLE)
    self.dvc.AppendTextColumn("Notation",   4, width=80,  mode=dv.DATAVIEW_CELL_EDITABLE)

    for c in self.dvc.Columns:
        c.Sortable = True
        c.Reorderable = True

    self.new = wx.Button(self.panel, -1, "New")
    self.Bind(wx.EVT_BUTTON, self.OnAddRow, self.new)

    self.modify = wx.Button(self.panel, -1, "Modify")
    self.Bind(wx.EVT_BUTTON, self.onModifyRow, self.modify)

    self.delete = wx.Button(self.panel, -1, "Delete")
    self.Bind(wx.EVT_BUTTON, self.OnDeleteRows, self.delete)

    self.close = wx.Button(self.panel, -1, "Close")
    self.Bind(wx.EVT_BUTTON, self.OnClosePanel, self.close)

    dvcSizer = wx.BoxSizer(wx.HORIZONTAL) 
    dvcSizer.Add(self.dvc, 1, wx.EXPAND)    

    btnbox = wx.BoxSizer(wx.VERTICAL)

    btnbox.Add(self.new, 0, wx.LEFT|wx.RIGHT, 5)
    btnbox.Add(self.modify, 0, wx.LEFT|wx.RIGHT, 5)
    btnbox.Add(self.delete, 0, wx.LEFT|wx.RIGHT, 5)
    btnbox.Add(self.close, 0, wx.LEFT|wx.RIGHT, 5)
    dvcSizer.Add(btnbox, 0, wx.TOP|wx.BOTTOM, 5)

    self.panel.SetSizer(dvcSizer)
    self.ShowModal()


def OnDeleteRows(self, evt):
    items = self.dvc.GetSelections()
    rows = [self.model.GetRow(item) for item in items]
    self.model.DeleteRows(rows)

Solution

  • It seems that you used the DVC_IndexListModel from the wxPython Demo. Did you implement the method DeleteRows(self, rows) as shown in the TestModel derived from PyDataViewIndexListModel? This would be your UpdateModel.

    BTW, 2.7.8 should be your (C)Python version. I suppose you have a wxPython version > 2.9.