在wx.grid wxpython中使用鼠标悬停在单元格上时的工具提示消息

时间:2013-12-15 00:00:36

标签: grid wxpython tooltip mousehover

我有一个wx.grid表,我想在我悬停在一个单元格上时设置一个工具提示,我在下面尝试了Mike Driscoll的建议,它有效,但我不能用鼠标拖动选择多个单元格,它允许我要选择最多1个单元格,请帮助:

self.grid_area.GetGridWindow().Bind(wx.EVT_MOTION, self.onMouseOver)

    def onMouseOver(self, event):
        '''
        Method to calculate where the mouse is pointing and
        then set the tooltip dynamically.
        '''

        # Use CalcUnscrolledPosition() to get the mouse position
        # within the
        # entire grid including what's offscreen
        x, y = self.grid_area.CalcUnscrolledPosition(event.GetX(),event.GetY())

        coords = self.grid_area.XYToCell(x, y)
        # you only need these if you need the value in the cell
        row = coords[0]
        col = coords[1]
        if self.grid_area.GetCellValue(row, col):
            if self.grid_area.GetCellValue(row, col) == "ABC":
                event.GetEventObject().SetToolTipString("Code is abc")
            elif self.grid_area.GetCellValue(row, col) == "XYZ":
                event.GetEventObject().SetToolTipString("code is xyz")
            else:
                event.GetEventObject().SetToolTipString("Unknown code")   

2 个答案:

答案 0 :(得分:5)

好的,我找到了解决方案,我必须跳过这个事件:

def onMouseOver(self, event):
        '''
        Method to calculate where the mouse is pointing and
        then set the tooltip dynamically.
        '''

        # Use CalcUnscrolledPosition() to get the mouse position
        # within the
        # entire grid including what's offscreen
        x, y = self.grid_area.CalcUnscrolledPosition(event.GetX(),event.GetY())

        coords = self.grid_area.XYToCell(x, y)
        # you only need these if you need the value in the cell
        row = coords[0]
        col = coords[1]
        if self.grid_area.GetCellValue(row, col):
            if self.grid_area.GetCellValue(row, col) == "ABC":
                event.GetEventObject().SetToolTipString("Code is abc")
            elif self.grid_area.GetCellValue(row, col) == "XYZ":
                event.GetEventObject().SetToolTipString("code is xyz")
            else:
                event.GetEventObject().SetToolTipString("Unknown code")   
        event.Skip()

由于 最好的问候

答案 1 :(得分:1)

@ GreenAsJade 由于我不能发表评论,我在这里回答你的问题!

为什么:出了什么问题,这是如何解决的?

如果你检查你的事件Hanlder和@ alwbtc的事件处理程序之间的区别只有区别是event.Skip()

每当wx.EVT_xx与代码中的自定义方法绑定时,wxpython都会覆盖默认定义。因此,事件处理以onMouseOver结束。 event.Skip()会将事件传播到_core的wxptyhon,允许它执行默认的事件处理程序。

希望这能回答你的问题!