Hi,
I missed Robin’s and Mike’s answer to a previous question on the subject, and thus posted “Horizontal scrolling wreak havoc on grid cell’s tooltips”, unaware that they already answered it.
Sorry.
For completeness sake, I’m posing the working example of how I implemented tooltips for individual grid’s cells, that takes into account scrolling.
The function to watch follows, where the red line is the one taking care of scrolling:
def __updateToolTip(self, event):
(x, y) = self.CalcUnscrolledPosition(event.GetX(),event.GetY())
lastCell = self.CellUnderMouse
self.CellUnderMouse = self.XYToCell(x,y).Get()
if self.CellUnderMouse <> lastCell:
(y,x) = self.CellUnderMouse
if 'tooltip' in self.contents[y][x].keys():
text = self.contents[y][x]['tooltip']
else:
text = '?'
self.GetGridWindow().SetToolTipString(text)
event.Skip()
(I guess all the above could be avoided if there was a way to treat a grid’s cell as an object, and thus attach the tooltip to the cell directly, without the need for mathematical gymnastics to convert mouse (x,y) coordinates to grid cell (row,col) coordinates)
Bye,
Ron.
#!/usr/bin/env python
import copy
import string
import wx
import wx.grid
class MyGrid(wx.grid.Grid):
def init(self, parent, data_list):
wx.grid.Grid.init(self, parent, wx.ID_ANY)
self.parent = parent
# set the rows and columns the grid needs
self.rows = len(data_list)
self.cols = len(data_list[0])
self.CreateGrid(self.rows, self.cols)
# set the tooltip array
self.contents = []
col_list = []
for col in range(self.cols):
col_list.append({})
for row in range(self.rows):
_list = copy.deepcopy(col_list) # explanation at:
# [http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_2.0/More_on_Lists](http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_2.0/More_on_Lists)
self.contents.append(_list)
for row in range(self.rows):
for col in range(self.cols):
tooltip = "[%d,%d]" % (row,col)
self.SetToolTipString(row, col, tooltip)
# set some column widths (default is 80) different
self.SetColSize(0, 180)
self.SetColSize(3, 100)
self.SetRowLabelSize(140) # sets leading row width to force scrolling
# set column lable titles at the top
for ix, title in enumerate(data_list[0]):
self.SetColLabelValue(ix, title)
# create reusable attribute objects
self.attr = wx.grid.GridCellAttr()
self.attr.SetTextColour('black')
self.attr.SetBackgroundColour('yellow')
#self.attr.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL))
# select the cell with a mouse left click
self.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK, self.onCellLeftClick)
self.CellUnderMouse = (-1,-1)
self.GetGridWindow().Bind(wx.EVT_MOTION, self.__updateToolTip)
self.loadCells(data_list)
def __updateToolTip(self, event):
(x, y) = self.CalcUnscrolledPosition(event.GetX(),event.GetY())
lastCell = self.CellUnderMouse
self.CellUnderMouse = self.XYToCell(x,y).Get()
if self.CellUnderMouse <> lastCell:
(y,x) = self.CellUnderMouse
if 'tooltip' in self.contents[y][x].keys():
text = self.contents[y][x]['tooltip']
else:
text = '?'
self.GetGridWindow().SetToolTipString(text)
event.Skip()
def SetToolTipString(self, row, col, tooltip):
self.contents[row][col]['tooltip'] = tooltip
def onCellLeftClick(self, event):
row = event.GetRow()
col = event.GetCol()
self.parent.SetTitle("row=%d col=%d value=%s" %
(row, col, self.GetCellValue(row, col)))
# move the grid's cursor to frame the cell
self.SetGridCursor(row, col)
def loadCells(self, data_list):
# note that title row is taken
for row in range(1, self.rows):
# set cell attributes for the whole row
self.SetRowAttr(row-1, self.attr)
for col in range(self.cols):
value = data_list[row][col]
self.SetCellValue(row-1, col, value)
self.SetReadOnly(row-1, col, True)
if col > 0:
self.SetCellAlignment(row-1, col,
wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
self.SetCellTextColour(row, 0, 'red')
self.SetCellBackgroundColour(row, 0, 'white')
self.SetCellFont(row, 0, wx.Font(8, wx.ROMAN, wx.ITALIC, wx.NORMAL))
build the data_list, raw_data string is from a csv file …
raw_data = “”"
Solvent Name, BP (deg C), MP (deg C), Density (g/ml)
ACETIC ACID,117.9,16.7,1.049
ACETIC ANHYDRIDE,140.1,-73.1,1.087
ACETONE,56.3,-94.7,0.791
XYLENES,139.1,-47.8,0.86"""
data_list = []
for line in raw_data.split(’\n’):
line_list = line.split(’,’)
data_list.append(line_list)
app = wx.App(0)
create a window/frame, no parent, use a default ID
title = “Demonstrating how horizontal scrolling interferes with tooltips placement”
frame = wx.Frame(None, wx.ID_ANY, title, size=(520, 360))
create the class instance
mygrid = MyGrid(frame, data_list)
frame.Show(True)
app.MainLoop()