[Example] Implementing tooltips for individual grid's cells Horizontal scrolling wreak havoc on grid cell's tooltips)

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()

Hi,

After implementing tooltips for individual grid cells, I want to implement tooltips for the grid’s column headings.

However, the method I used for the cell’s tooltips (i.e., binding to the wx.EVT_MOTION event) does not work,

as these events seem to be generated only for the grid cells, not the headings.

Can anyone suggest how to implement tooltips for grid (column) headings ?

Thanks,

Ron.

wxPython-users.comverse@9ox.net schrieb:

After implementing tooltips for individual grid cells, I want to implement tooltips for the grid's column headings.
However, the method I used for the cell's tooltips (i.e., binding to the wx.EVT_MOTION event) does not work,
as these events seem to be generated only for the grid cells, not the headings.
Can anyone suggest how to implement tooltips for grid (column) headings ?

Use these:

wx.EVT_MOTION(self.GetGridRowLabelWindow(), self.OnMouseMotionRowLabel)
wx.EVT_MOTION(self.GetGridColLabelWindow(), self.OnMouseMotionColLabel)

     def OnMouseMotionColLabel(self, evt):
         x, y = self.CalcUnscrolledPosition(evt.GetPosition())
         col = self.XToCol(x)
         label = self.table().GetColHelpValue(col)
         self.GetGridColLabelWindow().SetToolTipString(label or "")
         evt.Skip()

     def OnMouseMotionRowLabel(self, evt):
         x, y = self.CalcUnscrolledPosition(evt.GetPosition())
         row = self.YToRow(y)
         label = self.table().GetRowHelpValue(row)
         self.GetGridRowLabelWindow().SetToolTipString(label or "")
         evt.Skip()

Regards,

Dietmar

Hello! I've just created a wxPython application to do some file conversions to an RTF format. I have the RTF format working nicely except there are these pesky ’ replacing all my quote marks. I've googled extensively but I haven't found anything to help me get them out of my RTF file. (Plenty dealing with html files and such, but not this). I've tried to tinker with it on the RTF side and on the Python side and I'm flummoxed. I wonder if anyone out there would be willing to help me out on this?

I'm taking data from a Java application called ELAN and it is producing a tab delimited format. My program takes various input and then uses the tab delimited data to create an RTF "report" of sorts.

Thanks,

Stuart

I myself don’t know how to help you, but once you get this figured out,
I wonder if you would have any interest in working on converting the
RichTextCtrl’s format to RTF, for saving and for copying to the clipboard?

That would be a nice enhancement, and perhaps due to your understanding
of converting to RTF you could have some expertise in how to do that.

Thanks,
Che

···

On Wed, Aug 12, 2009 at 6:23 PM, Thiessen Stuart thiessenstuart@gmail.com wrote:

Hello! I’ve just created a wxPython application to do some file

conversions to an RTF format. I have the RTF format working nicely

except there are these pesky ’ replacing all my quote marks. I’ve

googled extensively but I haven’t found anything to help me get them

out of my RTF file. (Plenty dealing with html files and such, but not

this). I’ve tried to tinker with it on the RTF side and on the Python

side and I’m flummoxed. I wonder if anyone out there would be willing

to help me out on this?

I’m taking data from a Java application called ELAN and it is

producing a tab delimited format. My program takes various input and

then uses the tab delimited data to create an RTF “report” of sorts.

Thanks,

Stuart

Well, I cheated on the RTF. :slight_smile: There is a standard format that we use for this particular report. So, I simply looked at the RTF codes and the RTF specs and managed to get something set up so that I could simply load my data into that “template”. What I did would not work well for freestyle RTF. But I will say this. RTF is very picky, and sometimes an RTF file will work with one program and not another. I don’t like RTF as a format, but it is the closest thing I could do to a Word doc format which is what they preferred. (Sigh).

Now, I just have to get that bug fixed with the strange characters (it looks like this: pastedGraphic.tiff|x)

Thanks,

Stuart

···

On Aug 12, 2009, at 17:50 , C M wrote:

On Wed, Aug 12, 2009 at 6:23 PM, Thiessen Stuart thiessenstuart@gmail.com wrote:

Hello! I’ve just created a wxPython application to do some file
conversions to an RTF format. I have the RTF format working nicely
except there are these pesky ’ replacing all my quote marks. I’ve
googled extensively but I haven’t found anything to help me get them
out of my RTF file. (Plenty dealing with html files and such, but not
this). I’ve tried to tinker with it on the RTF side and on the Python
side and I’m flummoxed. I wonder if anyone out there would be willing
to help me out on this?

I’m taking data from a Java application called ELAN and it is
producing a tab delimited format. My program takes various input and
then uses the tab delimited data to create an RTF “report” of sorts.

Thanks,

Stuart

I myself don’t know how to help you, but once you get this figured out,
I wonder if you would have any interest in working on converting the
RichTextCtrl’s format to RTF, for saving and for copying to the clipboard?
That would be a nice enhancement, and perhaps due to your understanding
of converting to RTF you could have some expertise in how to do that.

Thanks,
Che

–~–~---------~–~----~------------~-------~–~----~
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en
-~----------~----~----~----~------~----~------~–~—

Stuart,

Well, I cheated on the RTF. :slight_smile: There is a standard format that we use for this particular report. So, I simply looked at the RTF codes and the RTF specs and managed to get something set up so that I could simply load my data into that “template”. What I did would not work well for freestyle RTF. But I will say this. RTF is very picky, and sometimes an RTF file will work with one program and not another. I don’t like RTF as a format, but it is the closest thing I could do to a Word doc format which is what they preferred. (Sigh).

Now, I just have to get that bug fixed with the strange characters (it looks like this: pastedGraphic.tiff|x)

Thanks,

Stuart

Oops…I emailed you personally from my work address as I guess you’re a part of the local LUG group. Anyway, I think it would be a good idea to try the comp.lang.python guys: http://groups.google.com/group/comp.lang.python/topics

They might have some better ideas. Or you could copy the text and use win32com to paste it into a hidden MS Word instance and then use Word to save the text. Then you wouldn’t even need to mess with RTF.

  • Mike
···

On Wed, Aug 12, 2009 at 6:05 PM, Thiessen Stuart thiessenstuart@gmail.com wrote:

On Aug 12, 2009, at 17:50 , C M wrote:

On Wed, Aug 12, 2009 at 6:23 PM, Thiessen Stuart thiessenstuart@gmail.com wrote:

Hello! I’ve just created a wxPython application to do some file
conversions to an RTF format. I have the RTF format working nicely

except there are these pesky ’ replacing all my quote marks. I’ve
googled extensively but I haven’t found anything to help me get them
out of my RTF file. (Plenty dealing with html files and such, but not

this). I’ve tried to tinker with it on the RTF side and on the Python
side and I’m flummoxed. I wonder if anyone out there would be willing
to help me out on this?

I’m taking data from a Java application called ELAN and it is

producing a tab delimited format. My program takes various input and
then uses the tab delimited data to create an RTF “report” of sorts.

Thanks,

Stuart

I myself don’t know how to help you, but once you get this figured out,

I wonder if you would have any interest in working on converting the
RichTextCtrl’s format to RTF, for saving and for copying to the clipboard?
That would be a nice enhancement, and perhaps due to your understanding

of converting to RTF you could have some expertise in how to do that.

Thanks,
Che

Mike Driscoll

Blog: http://blog.pythonlibrary.org

Thiessen Stuart wrote:

Hello! I've just created a wxPython application to do some file conversions to an RTF format. I have the RTF format working nicely except there are these pesky ’ replacing all my quote marks. I've googled extensively but I haven't found anything to help me get them out of my RTF file. (Plenty dealing with html files and such, but not this). I've tried to tinker with it on the RTF side and on the Python side and I'm flummoxed. I wonder if anyone out there would be willing to help me out on this?

Those rectangles usually mean that there isn't a glyph in the current font for the character you are trying to display. What is the font being used and what is the character code that fails? Perhaps it is some obscure unicode glyph that doesn't have common font support, but you can probably substitute another quote character.

···

--
Robin Dunn
Software Craftsman

Stuart,

Thiessen Stuart wrote:

Hello! I've just created a wxPython application to do some file conversions to an RTF format. I have the RTF format working nicely except there are these pesky ’ replacing all my quote marks. I've googled extensively but I haven't found anything to help me get them out of my RTF file. (Plenty dealing with html files and such, but not this). I've tried to tinker with it on the RTF side and on the Python side and I'm flummoxed. I wonder if anyone out there would be willing to help me out on this?

I'm taking data from a Java application called ELAN and it is producing a tab delimited format. My program takes various input and then uses the tab delimited data to create an RTF "report" of sorts.
  

Have you looked at:
http://pyrtf.sourceforge.net/

Doesn't look actively maintained but maybe there are still some hints/ideas to get from it.

Werner

Hi, Mike. I wondered if you were the same person. :slight_smile:

Using win32com is not really a solution for us because many of us use Macs and not everyone has Office for Mac. At least one of us uses Linux. So, I was trying to find a Python only implementation. :slight_smile:

Worked on it last night and finally figured out a solution. Several comments from others focused on the encoding issue, so I did a hex dump of the export file and found out what the codes were and then used a replace (data.replace(‘\u____’, u’\x__') to switch for the equivalent in the lower ASCII set. That did the trick to avoid any of the strange symbols.

I did try pyrtf but it kept crashing Word or OpenOffice. So I just downloaded the spec for RTF and then took one of our files, converted it to RTF and tried to find the codes that I needed for this specific implementation. Not as flexible, but it works for the purpose it was needed for … for now. :slight_smile:

Thanks for your feedback! They helped me get onto the right track. :slight_smile:

Stuart

···

On Aug 12, 2009, at 21:58 , Mike Driscoll wrote:

Stuart,

On Wed, Aug 12, 2009 at 6:05 PM, Thiessen Stuart thiessenstuart@gmail.com wrote:

Well, I cheated on the RTF. :slight_smile: There is a standard format that we use for this particular report. So, I simply looked at the RTF codes and the RTF specs and managed to get something set up so that I could simply load my data into that “template”. What I did would not work well for freestyle RTF. But I will say this. RTF is very picky, and sometimes an RTF file will work with one program and not another. I don’t like RTF as a format, but it is the closest thing I could do to a Word doc format which is what they preferred. (Sigh).

Now, I just have to get that bug fixed with the strange characters (it looks like this: <pastedGraphic.tiff>)

Thanks,

Stuart

Oops…I emailed you personally from my work address as I guess you’re a part of the local LUG group. Anyway, I think it would be a good idea to try the comp.lang.python guys: http://groups.google.com/group/comp.lang.python/topics

They might have some better ideas. Or you could copy the text and use win32com to paste it into a hidden MS Word instance and then use Word to save the text. Then you wouldn’t even need to mess with RTF.

  • Mike

On Aug 12, 2009, at 17:50 , C M wrote:

On Wed, Aug 12, 2009 at 6:23 PM, Thiessen Stuart thiessenstuart@gmail.com wrote:

Hello! I’ve just created a wxPython application to do some file
conversions to an RTF format. I have the RTF format working nicely
except there are these pesky ’ replacing all my quote marks. I’ve
googled extensively but I haven’t found anything to help me get them
out of my RTF file. (Plenty dealing with html files and such, but not
this). I’ve tried to tinker with it on the RTF side and on the Python
side and I’m flummoxed. I wonder if anyone out there would be willing
to help me out on this?

I’m taking data from a Java application called ELAN and it is
producing a tab delimited format. My program takes various input and
then uses the tab delimited data to create an RTF “report” of sorts.

Thanks,

Stuart

I myself don’t know how to help you, but once you get this figured out,
I wonder if you would have any interest in working on converting the
RichTextCtrl’s format to RTF, for saving and for copying to the clipboard?
That would be a nice enhancement, and perhaps due to your understanding
of converting to RTF you could have some expertise in how to do that.

Thanks,
Che

Mike Driscoll

Blog: http://blog.pythonlibrary.org

Hi Dietmar,

I implemented your suggestions above (code below), but in my solution,
the mouse events are generated only when the mouse is in the proper
grid area.
If the mouse is on a label, no events are generated (namely, the print
line is not activated).

Is my coding wrong ?

Bye,
Ron.

    def __updateToolTip(self, event):
        (x, y) = self.grid.CalcUnscrolledPosition(event.GetX
(),event.GetY()) # scrolling is taken

# into account.
        lastCell = self.CellUnderMouse
        self.CellUnderMouse = self.grid.XYToCell(x,y).Get()
        self.ColUnderMouse = self.grid.XToCol(x)
        print "self.ColUnderMouse:",self.ColUnderMouse
        if self.CellUnderMouse <> lastCell:
            (y,x) = self.CellUnderMouse
            if self.tooltips.has_key(self.req_table) and 'tooltip' in
self.tooltips[self.req_table][y].keys():
                text = self.tooltips[self.req_table][y]['tooltip']
            else:
                text = ""
            self.grid.GetGridWindow().SetToolTipString(text)
        event.Skip()

···

On Aug 9, 2:14 pm, Dietmar Schwertberger <maill...@schwertberger.de> wrote:

wxPython-users.comve...@9ox.net schrieb:

> After implementing tooltips for individualgridcells, I want to
> implement tooltips for thegrid's column headings.
> However, the method I used for the cell's tooltips (i.e., binding to the
> wx.EVT_MOTION event) does not work,
> as these events seem to be generated only for thegridcells, not the
> headings.

> Can anyone suggest how to implement tooltips forgrid(column) headings ?

Use these:

wx.EVT_MOTION(self.GetGridRowLabelWindow(), self.OnMouseMotionRowLabel)
wx.EVT_MOTION(self.GetGridColLabelWindow(), self.OnMouseMotionColLabel)

 def OnMouseMotionColLabel\(self, evt\):
     x, y = self\.CalcUnscrolledPosition\(evt\.GetPosition\(\)\)
     col = self\.XToCol\(x\)
     label = self\.table\(\)\.GetColHelpValue\(col\)
     self\.GetGridColLabelWindow\(\)\.SetToolTipString\(label or &quot;&quot;\)
     evt\.Skip\(\)

 def OnMouseMotionRowLabel\(self, evt\):
     x, y = self\.CalcUnscrolledPosition\(evt\.GetPosition\(\)\)
     row = self\.YToRow\(y\)
     label = self\.table\(\)\.GetRowHelpValue\(row\)
     self\.GetGridRowLabelWindow\(\)\.SetToolTipString\(label or &quot;&quot;\)
     evt\.Skip\(\)

Regards,

Dietmar