This experimental code shows how to create a HitTest method for a wxGrid. With this code, you can test which cell is under your mouse pointer. It is important that the x,y coordinates needs to be the client coordinates of wx.Grid.GetGridWindow(), not the grid itself.

   1 import wx.grid as  gridlib
   2 
   3 class MegaGrid(gridlib.Grid):           
   4         def HitCellTest(self,x,y):
   5                 """Convert client coordinates to cell position.
   6                 
   7                 @return: a tuple of (row,col). If the position is not over a column or row,
   8                         then it will have a value of -1.
   9                         
  10                 Note: client coordinates are client coordinats of wx.Grid.GetGridWindow(), not
  11                         the grid itself!
  12                 """
  13                 x, y = self.CalcUnscrolledPosition(x, y)
  14                 return self.XToCol(x),self.YToRow(y)

Usage example:

   1 class MyFrame(wx.Frame):
   2     def __init__(self,*args,**kwargs):
   3         wx.Frame.__init__(self,*args,**kwargs)
   4         self.grid = wx.grid.Grid()
   5         self.grid.GetGridWindow().Bind(
   6             wx.EVT_MOTION,
   7             self.OnMotion,
   8             self.grid.GetGridWindow()
   9         )
  10 
  11     def OnMotion(self,event):
  12         # Tell which cell is below your mouse pointer
  13         self.curcol, self.currow = self.grid.HitCellTest(event.GetX(),event.GetY())

wxGridHitTest (last edited 2008-03-11 10:50:31 by localhost)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.