wx.Grid and ToolTip in single cells

While I have no problem setting a distinctive tooltip on a single column, row or label of a grid, it has been tough to do it for a single cell because I didn’t know how to combine row and col in a key for a dictionary: I tried in vain with tuples, lists, even a wx.Point.
My solution, if anybody is interested, follows:

    def SetCellTooltip(self, row, col, tooltip):
        assert type(row) == int, type(col) == int
        cell = wx.Point(row, col)
        cellkey = str(row).zfill(4) + str(col).zfill(2)
        if tooltip == "":
            if key in self.CellToolTips:
                del self.CellToolTips[cellkey]
           if cell in self.TooltippedCells:
                del self.TooltippedCells[cell]
        else:
            if not cell in self.TooltippedCells:
                self.TooltippedCells.append(cell)
            self.CellToolTips[cellkey] = tooltip

 def OnMouseMotion(self, event):
        event.Skip()
        if len(self.CellToolTips) == len(self.ColToolTips) == \
        len(self.RowToolTips) == 0: return
        pos = event.GetPosition()
        coords = self.XYToCell(pos.x, pos.y)
        row = coords[0]
        col = coords[1]
        cell = wx.Point(row, col)
        key = str(row).zfill(4) + str(col).zfill(2)      
        new_tooltip = self.GetToolTip()
        if coords[0] in self.RowToolTips: new_tooltip = self.RowToolTips[coords[0]]
        if coords[1] in self.ColToolTips:  new_tooltip = self.ColToolTips[coords[1]]
        if cell in self.TooltippedCells:         new_tooltip = self.CellToolTips[key]
        if not coords[0] in self.RowToolTips and not coords[1] in \
                self.ColToolTips and not cell in self.TooltippedCells:
            new_tooltip = self.DefaultToolTip

        if self.GetToolTip() != new_tooltip:
            self.ChangeToolTip(new_tooltip)

  def ChangeToolTip(self, value):
       assert value is None or type(value) is str
       gw = self.GetGridWindow()
       if value is None:   
            gw.SetToolTip(None)
       else:  
            TT = gw.GetToolTip()
            if not TT is None:
                TT.SetTip(value)
            else: 
               TT = wx.ToolTip(value)
               TT.SetDelay(700)
               gw.SetToolTip(TT)

It works, but even to its beloved Dad (me) it looks like spaghetti code. If anyone has a more straight solution, I will happily comply.

raffaello wrote:

While I have no problem setting a distinctive tooltip on a single column, row or label of a grid, it has been tough to do it for a single cell because I didn't know how to combine row and col in a key for a dictionary: I tried in vain with tuples, lists, even a wx.Point.

A tuple is an immutable object and so it is a perfectly valid object to use for a dictionary key.

  >>> d = {}
  >>>
  >>> d[(1,2)] = "one-two"
  >>> d[(3,4)] = "three-four"
  >>>
  >>> d
  {(1, 2): 'one-two', (3, 4): 'three-four'}
  >>> d[(1,2)]
  'one-two'
  >>> d[(2,1)]
  Traceback (most recent call last):
    File "<input>", line 1, in <module>
  KeyError: (2, 1)

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!