Search code examples
pythonwxwidgetswxpython

wxPython dataviewcustomrenderer Left click event won't fire


I have a DataViewListCtrl where I would like to have a column of buttons. To achieve this I am using the following custom renderer:

class DataViewButtonRenderer( dv.DataViewCustomRenderer ):

def __init__(self, log, *args, **kw):
    dv.DataViewCustomRenderer.__init__(self, *args, **kw)
    self.log = log
    self.down = False
    self.value = None

def SetValue(self, value):
    self.value = value
    return True

def GetValue(self):
    return self.value

def GetSize(self):
    value = self.value if self.value else ""
    size = self.GetTextExtent(value * 2)
    return size

def Render(self, rect, dc, state):
    dc.SetBrush( wx.LIGHT_GREY_BRUSH )
    dc.SetPen( wx.TRANSPARENT_PEN )

    rect.Deflate(2)

    dc.DrawRoundedRectangle( rect, 4 )
    value = self.value if self.value else ""
    self.RenderText(value, 15, rect, dc, state)

    if self.down and not self.click_handled:
        self.click_handled = True
        self.ActivateCell()

    return True

def GetValueFromEditorCtrl(self, editor):
    self.log.write('GetValueFromEditorCtrl: %s' % editor)
    value = editor.GetValue()
    return True, value

def LeftClick(self, pos, cellRect, model, item, col):
    self.log.write('LeftClick')
    print "Left Click"
    return False

def ActivateCell(self, cell, model, item, col, event=wx.MouseEvent):
    self.log.write('ActivateCell')
    print "Button clicked"
    return False

But how do I get the left click event to actually fire? I have tried using self.Bind in the init function but that gives me an error that 'DataViewButtonRenderer' object has no attribute 'Bind', aka DataViewCustomRenderer has no Bind method. How can I bind the event?


Solution

  • Renderers are only responsible for rendering, i.e. drawing, the cell contents. For changing it, you have 2 possibilities: either make the cell activatable and then you can react to wxEVT_DATAVIEW_ITEM_ACTIVATED events, generated by double clicking the cell or from keyboard; or make it editable and then you can use a custom editor for it by overriding CreateEditorCtrl() method in your renderer.