GenStaticText control and tooltips

Robin Dunn wrote (in 2004):

jf gosset wrote:
> Hi,

>
> The association of a tooltip and a wxStaticText is accepted:
> myStaticText.SetToolTipString
("blah blah")
> But the tooltip never appaers.
>
> Is it a limitation or a bug ?


A limitation. The wxStaticText isn't able to get all the mouse events.

>
> It's rather possible to use a workaround: place the StaticText in a panel
> and attribute the tooltip to the panel.

Yes that is one way. Another is to use the generic static text control
located in wx.lib.stattext.

I ran into the same problem with wx.StaticText: it seems to block the working of tooltips on its parent panel. I have a bunch of wx.StaticText controls in sizers on a panel. There is nothing in the containment hierarchy (only sizers) in between the StaticTexts and their parent panel. I set a tooltip for the parent panel, and the tooltip pops up appropriately when the mouse is anywhere over the parent panel except if the mouse is over any of the StaticText controls.

So I searched and found the above message/response, and tried switching all of these controls to GenStaticText from wx.lib.stattext. Unfortunately the results appear exactly the same on my system: wxPython
2.8.4.0
(msw-unicode), Python 2.5.1. The parent panel’s tooltip still works only when the mouse pointer is not over any of the GenStaticText controls.

As for the other solution above, placing the static text controls on a panel and setting the tooltip on that panel, that’s exactly what I’m doing and that’s not working either.

What else do I need to do to make a tooltip work for an entire panel regardless of which of the panel’s children the mouse may be above?

Thanks!
Eric

Eric Ongerth wrote:

So I searched and found the above message/response, and tried switching all of these controls to GenStaticText from wx.lib.stattext. Unfortunately the results appear exactly the same on my system: wxPython 2.8.4.0 (msw-unicode), Python 2.5.1. The parent panel's tooltip still works only when the mouse pointer is not over any of the GenStaticText controls.

Give each of the static texts a tooltip too.

···

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

I suppose I could subclass either the regular StaticText or GenStaticText and upon the subclass’s init the instance will look for its parent’s tooltip, if any, and if found, adopt it. Or better, add an argument to the constructor such as “tooltip_transparent = True”. I think i’ll try that.

Thank you for the suggestion.

···

On 6/28/07, Robin Dunn robin@alldunn.com wrote:

Eric Ongerth wrote:

So I searched and found the above message/response, and tried switching
all of these controls to GenStaticText from wx.lib.stattext.
Unfortunately the results appear exactly the same on my system: wxPython

2.8.4.0 (msw-unicode), Python 2.5.1. The parent
panel’s tooltip still works only when the mouse pointer is not over any
of the GenStaticText controls.

Give each of the static texts a tooltip too.

Ok, so I did the following:

class SubStaticText(GenStaticText):

def __init__(self, parent, id, text, tooltip_passthru=True):
    GenStaticText.__init__(self, parent, id, text)

    if tooltip_passthru:
        self.SetToolTip(parent.GetToolTip())

pass

I then replaced all of the GenStaticText instances in a certain panel, with this new subclass. At first this appeared to work great. It did just what I wanted… the SubStaticText instances picked up the parent panel’s tooltip and displayed it properly. Everything was fine – until it was time to shut down that panel. Whether the panel was deleted by its own parent notebook, or by the closing of the application, Python itself crashed in a way that wasn’t caught/handled by my debugger even at its most sensitive setting. Going back and executing every statement, i drilled down to a place in the closing methods where apparently the panel’s sizers are getting deleted. The farthest I was able to follow before it disappeared behind the Python/C++ veil (?) was in the del method of class Sizer.

Commenting out the part in SubStaticText where self.SetToolTip is called, eliminates the problem (and eliminates the positive behavior that I was trying for too, unfortunately).

The following workaround works as desired:

class SubStaticText(GenStaticText):

def __init__(self, parent, id, text, tooltip_passthru=True):
    GenStaticText.__init__(self, parent, id, text)
    if tooltip_passthru:
        tooltip = wx.ToolTip(parent.GetToolTip().GetTip())
        self.SetToolTip(tooltip)

pass

So clearly I can’t just grab the parent window’s tooltip and reuse it; I have to instantiate a new tooltip that copies its text.

Should this be considered a bug or expected behavior? Seems to me the docs for wxToolTip should probably mention that the tooltip gets destroyed along with its parent window, and therefore cannot be reused without copying/cloning. Or alternatively, either wxToolTips should be reference-counted… or else they should clone themselves upon reuse, or something.

Not that I mind the simple, easy workaround. Just that it was alarming and took a while to trace where the crash was coming from since an exception wasn’t raised; Python crashed instead.

-Eric

···

On 6/28/07, Eric Ongerth ericongerth@gmail.com wrote:

Eric Ongerth wrote:

So I searched and found the above message/response, and tried switching
all of these controls to GenStaticText from wx.lib.stattext.
Unfortunately the results appear exactly the same on my system: wxPython

2.8.4.0 (msw-unicode), Python 2.5.1. The parent
panel’s tooltip still works only when the mouse pointer is not over any

of the GenStaticText controls.

Give each of the static texts a tooltip too.
On 6/28/07, Robin Dunn <robin@alldunn.com > > wrote:

I suppose I could subclass either the regular StaticText or GenStaticText and upon the subclass’s init the instance will look for its parent’s tooltip, if any, and if found, adopt it. Or better, add an argument to the constructor such as “tooltip_transparent = True”. I think i’ll try that.

Thank you for the suggestion.

Eric Ongerth wrote:

So clearly I can't just grab the parent window's tooltip and reuse it; I have to instantiate a new tooltip that copies its text.

Yep.

Should this be considered a bug or expected behavior?

A little of both actually.

Seems to me the docs for wxToolTip should probably mention that the tooltip gets destroyed along with its parent window, and therefore cannot be reused without copying/cloning.

You can pretty much assume that anything given to a window is owned by the window and will be destroyed when it is.

Or alternatively, either wxToolTips should be reference-counted... or else they should clone themselves upon reuse, or something.

Not that I mind the simple, easy workaround. Just that it was alarming and took a while to trace where the crash was coming from since an exception wasn't raised; Python crashed instead.

C/C++ has a tendency to do that when you mess around with a pointer to memory that has already been deallocated. They're probably the best shoot-yourself-in-the-foot languages of all time! :wink:

···

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