Search code examples
gridwxpythontooltipmousehover

Tooltip message when hovering on cell with mouse in wx.grid wxpython


I have a wx.grid table, I want to set a tooltip when I hover on a cell, I tried Mike Driscoll's recommendation below, it works, but I can't select multiple cells with mouse drag anymore, it allows me to select only 1 cell max, please help:

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")   

Solution

  • OK, I found the solution, I have to skip the event:

    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()
    

    Thanks best regards