Adding balloon tooltips to a grid column/cell ?

Hi Mike,

I started playing with your code, and evt.GetRow() and evt.GetCol() would surely be nice to have :wink:

Since, as you mention, one has to translate the evt.GetX to a column number, I wonder how you dealt with the user changing column widths ?

What I mean is that (in my program) I let the user change column widths, and they also change to accommodate different texts that need to be displayed.

It boils down to that I have no fixed mapping of X,Y coordinates to col,row coordinates.

How did you solve this issue ?

Bye,
Ron.

路路路

-----Original Message-----
From: Mike Driscoll [mailto:mike@pythonlibrary.org]
Sent: Wednesday, October 08, 2008 16:08
To: wxpython-users@lists.wxwidgets.org
Subject: Re: [wxpython-users] Adding balloon tooltips to a grid column/cell ?

Ron,

Hi,

I'm trying to implement ToolTips for a grid column.

Googling brought me to BalloonTip - wxPyWiki, but the
examples referred to there are in the non-existent page
(http://jump.xoom.alice.it/jump.htm).

Do any of you have a code snippet demonstrating how to add balloon
tooltips to a grid column/cell ?

Thanks,
Ron.

After re-reading this, I realized that I already do this in one of my applications using the default tooltips. In your application's __init__, you'll need to initialize your tooltip variable to an empty string:

self.tooltip = ""

Then to bind to the column area, do this:

self.myGrid.GetGridColLabelWindow().Bind(wx.EVT_MOTION, self.onMouseOver)

Finally, in my MouseOver event, I do something like this:

<code>

def onMouseOver(self, event):
        '''
        When the mouse is hovered over certain columns, a tooltip will be
        displayed to give the user more information about what data should
        go into that specific column
        '''
        pos = event.GetX()

        if pos > 130 and pos < 180:
            if self.tooltip != 'Regular Hours':

self.sheet.GetGridColLabelWindow().SetToolTipString('Regular Hours')
                self.tooltip = 'Regular Hours'
        elif pos > 180 and pos < 230:
            if self.tooltip != 'Overtime Hours':

self.sheet.GetGridColLabelWindow().SetToolTipString('Overtime Hours')
                self.tooltip = 'Overtime Hours'
        else:
            if self.tooltip != '':
                self.sheet.GetGridColLabelWindow().SetToolTipString('')
                self.tooltip = ''
        event.Skip()

</code>

You'll need to figure out the widths of your own columns yourself and change the numbers in the "if" statements accordingly, of course. I hope that helps!

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org

Ron,

Hi Mike,

I started playing with your code, and evt.GetRow() and evt.GetCol() would surely be nice to have :wink:

Since, as you mention, one has to translate the evt.GetX to a column number, I wonder how you dealt with the user changing column widths ?

What I mean is that (in my program) I let the user change column widths, and they also change to accommodate different texts that need to be displayed.

It boils down to that I have no fixed mapping of X,Y coordinates to col,row coordinates.

How did you solve this issue ?

Bye,
Ron.
  
If I recall correctly, my columns had no need to be resized, so I disallowed resizing. In your case, that obviously won't work. I assume you are storing the widths of your columns somehow, so just use that and some basic math to figure it out. If you're not storing the widths and the user has to resize them every time they run the program, then you'll just have to store them in some column width variables / attributes. Just catch the column change event and do it that way.

Mike

路路路

-----Original Message-----
From: Mike Driscoll [mailto:mike@pythonlibrary.org]
Sent: Wednesday, October 08, 2008 16:08
To: wxpython-users@lists.wxwidgets.org
Subject: Re: [wxpython-users] Adding balloon tooltips to a grid column/cell ?

Ron,

Hi,

I'm trying to implement ToolTips for a grid column.

Googling brought me to BalloonTip - wxPyWiki, but the
examples referred to there are in the non-existent page
(http://jump.xoom.alice.it/jump.htm).

Do any of you have a code snippet demonstrating how to add balloon
tooltips to a grid column/cell ?

Thanks,
Ron.
    
After re-reading this, I realized that I already do this in one of my applications using the default tooltips. In your application's __init__, you'll need to initialize your tooltip variable to an empty string:

self.tooltip = ""

Then to bind to the column area, do this:

self.myGrid.GetGridColLabelWindow().Bind(wx.EVT_MOTION, self.onMouseOver)

Finally, in my MouseOver event, I do something like this:

<code>

def onMouseOver(self, event):
        '''
        When the mouse is hovered over certain columns, a tooltip will be
        displayed to give the user more information about what data should
        go into that specific column
        '''
        pos = event.GetX()

        if pos > 130 and pos < 180:
            if self.tooltip != 'Regular Hours':

self.sheet.GetGridColLabelWindow().SetToolTipString('Regular Hours')
                self.tooltip = 'Regular Hours'
        elif pos > 180 and pos < 230:
            if self.tooltip != 'Overtime Hours':

self.sheet.GetGridColLabelWindow().SetToolTipString('Overtime Hours')
                self.tooltip = 'Overtime Hours'
        else:
            if self.tooltip != '':
                self.sheet.GetGridColLabelWindow().SetToolTipString('')
                self.tooltip = ''
        event.Skip()

</code>

You'll need to figure out the widths of your own columns yourself and change the numbers in the "if" statements accordingly, of course. I hope that helps!

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org