Reputation: 29445
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")
Upvotes: 5
Views: 3431
Reputation: 659
@ GreenAsJade Since I cannot comment due to reputation i am asnwering your question here!
why: what was going wrong, how does this fix it?
If you check the difference between your event Hanlder and @alwbtc's event Handler only difference is event.Skip()
Any time the wx.EVT_xx is bind with custom method in code, wxpython override default definition. For that reason event handling ends in onMouseOver. event.Skip() will propagate the event to _core of wxptyhon allowing it to execute default event handlers.
Hope this answers your question!
Upvotes: 1
Reputation: 29445
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
Upvotes: 6