wxPython dataviewcustomrenderer左键单击事件不会触发

时间:2018-06-07 14:52:49

标签: python wxpython wxwidgets wxpython-phoenix

我有一个DataViewListCtrl,我希望有一列按钮。为实现此目的,我使用以下自定义渲染器:

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

但是如何让左击事件真正发生?我尝试在 init 函数中使用self.Bind但是这给了我一个错误,'DataViewButtonRenderer'对象没有属性'Bind',又名DataViewCustomRenderer没有Bind方法。我该如何绑定事件?

1 个答案:

答案 0 :(得分:0)

渲染器仅负责渲染(即绘制)单元格内容。要更改它,您有两种可能:要么使单元格可激活,然后您可以对通过双击单元格或键盘生成的wxEVT_DATAVIEW_ITEM_ACTIVATED事件作出反应;或者使其可编辑,然后您可以通过在渲染器中覆盖CreateEditorCtrl()方法来使用自定义编辑器。