How to highlight grid row when hovering

Hello,

I have a 10 x 10 grid/table. I want to highlight the entire row when my cursor hovers (not select) over any cell of that row.

Take the image below for example, if my cursor lands on CELL(1,J), the entire row 1 should be coloured in cyan, and then return to the original colour when my cursor leaves any cell from that row. And this should happen in real time with any row that my cursor lands on.

I’m guessing I could use EVT_MOTION to do this but I’m not sure how? Please send help senpais :3

Hi Charis,

The EVT_MOTION is the correct way to go, but the object to bind the event is grid.GridWindow.
I’ve referenced some resources to create a simple example as follows:

I hope this is a good starting point for you!

import wx
import wx.grid

class Frame(wx.Frame):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.grid = wx.grid.Grid(self)
        self.grid.CreateGrid(5, 5)
        self.last_cell = None

        self.grid.GridWindow.Bind(wx.EVT_MOTION, self.OnMotion)

    def set_colour(self, cell, colour):
        attr = wx.grid.GridCellAttr()
        attr.SetBackgroundColour(colour)
        self.grid.SetRowAttr(cell[0], attr)

    def OnMotion(self, evt):
        x, y = self.grid.CalcUnscrolledPosition(evt.Position)
        cell = self.grid.XYToCell(x, y)
        if cell != self.last_cell:
            if self.last_cell is not None:
                self.set_colour(self.last_cell, wx.WHITE) # on-exit
            self.set_colour(cell, wx.CYAN) # on-enter
            self.grid.Refresh()
        self.last_cell = cell
        evt.Skip()

app = wx.App()
frm = Frame(None)
frm.Show()
app.MainLoop()

Clipboard01

1 Like

Hi Komoto,

It works perfectly!! Thank you so so much!!!