You can set the tooltip for a wxGrid window as a whole using the SetToolTip method, but how do you set different tooltips for individual cells of the grid?

Here's how: Have a mouse event check every mouse move to see if you've moved between cells, and change the tooltip when you have.

The Code

Here's a function to do the hard work. Pass the grid along with a callback which takes row and column parameters and returns a tooltip string for that cell. InstallGridHint will do the rest.

   1 def InstallGridHint(grid, rowcolhintcallback):
   2     prev_rowcol = [None,None]
   3     def OnMouseMotion(evt):
   4         # evt.GetRow() and evt.GetCol() would be nice to have here,
   5         # but as this is a mouse event, not a grid event, they are not
   6         # available and we need to compute them by hand.
   7         x, y = grid.CalcUnscrolledPosition(evt.GetPosition())
   8         row = grid.YToRow(y)
   9         col = grid.XToCol(x)
  11         if (row,col) != prev_rowcol and row >= 0 and col >= 0:
  12             prev_rowcol[:] = [row,col]
  13             hinttext = rowcolhintcallback(row, col)
  14             if hinttext is None:
  15                 hinttext = ''
  16             grid.GetGridWindow().SetToolTipString(hinttext)
  17         evt.Skip()
  19     wx.EVT_MOTION(grid.GetGridWindow(), OnMouseMotion)

wxGrid ToolTips (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.