Copy and try the code here below:
import wx, wx.grid
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, parent = None, id = -wx.ID_ANY,
style = wx.MINIMIZE_BOX | wx.CLOSE_BOX| wx.SYSTEM_MENU\
>wx.CAPTION | wx.CLIP_CHILDREN)
self.panel = wx.Panel(self)
self.mainsizer = wx.BoxSizer(wx.VERTICAL)
self.panel.SetSizer(self.mainsizer)
self.grid = wx.grid.Grid(self.panel, wx.ID_ANY )
self.mainsizer.Add(self.grid, 1, wx.ALL | wx.GROW, 2)
self.grid.CreateGrid(10, 6)
self.grid.GetTable().SetValue(2, 1, 'row 3, col B')
self.grid.GetGridWindow().Bind(wx.EVT_MOTION, self.OnMouseMotion)
self.Layout()
self.mainsizer.SetSizeHints(self)
self.mainsizer.Fit(self)
self.Fit()
self.grid.DefaultToolTip = ''
self.Show()
self.grid.AppendRows(5)
def OnMouseMotion(self, event):
event.Skip()
pos = event.GetPosition()
coords = self.grid.XYToCell(pos.x, pos.y)
cell = coords[0], coords[1]
if cell == (2, 1):
self.grid.GetGridWindow().SetToolTipString('This is the tooltip of cell(3, B)')
else:
self.grid.GetGridWindow().SetToolTip(None)
if name == ‘main’:
app = wx.PySimpleApp()
frame = TestFrame()
app.MainLoop()
If you pass with your mouse over cell(2, 1), you’ll see the proper tooltip.
Now, scroll the grid a bit downward , so that the cell(2, 1) is still visible, and retry: you will not see the tooltip in the cell (3, B), but somehow below, as if the coordinates referred not to the cell but to an absolute position on the grid.
Any suggestion?