Search code examples
python-2.7wxpythonwxwidgets

How to remove a tooltip in wxpython?


How do I remove a tooltip in wxpython? I know they have an expiry time after which they disappear but I'm struggling to find anything in the docs about purposefully removing them.

In my particular situation, I have a wx.grid which on wx.EVT_MOTION calls a function, that gets the coordinates of the event in the grid, finds out which cell it is (a la grid.CalcUnscrolledPosition() and grid.XYToCell()) and then, provided it is a particular cell, creates a tooltip. Now, in the case there is a tooltip already, I want to remove it if the cursor hovers above a cell that doesn't require a tooltip. I'm struggling to find anything in the documentation. This link has the ToolTip.Enable() function, but that has no result whatsoever. The docs seem to imply that it should enable/disable tooltips globally but nothing happens.

Any ideas?

FYI, Win7, Python 2.7.9, wx 3.0.2.0

EDIT Rough structure of relevant code:

self.grid.GetGridWindow().Bind(wx.EVT_MOTION, self.onMouseOver)

...

def onMouseOver(self, event):
    #get current grid cell
    x, y = self.grid.CalcUnscrolledPosition(event.GetX(), event.GetY())
    coords = self.grid.XYToCell(x,y)
    row = coords[0]
    col = coords[1]

    if col in relevanCols:
        event.GetEventObject().SetToolTipString("Hai")
    else:
        self.grid.SetToolTip(None)

    event.Skip()

Solution

  • Finally, after beasting the damned thing for an age, a result and mind bogglingly simple as well SetToolTip(None)
    See below:

    import wx
    class MyForm(wx.Frame):
        def __init__(self):
            wx.Frame.__init__(self, None, wx.ID_ANY, "Toggle")
            self.panel = wx.Panel(self, wx.ID_ANY)
            self.dummy = wx.StaticText(self.panel, -1, "")
            self.cb1 = wx.CheckBox(self.panel, -1,"Choice 1", pos=(10,10))
            self.cb2 = wx.CheckBox(self.panel, -1,"Choice 2", pos=(10,40))
            self.cb1.Bind(wx.EVT_CHECKBOX, self.onToggle1)
            self.cb2.Bind(wx.EVT_CHECKBOX, self.onToggle2)
            self.cb1.SetToolTipString(wx.EmptyString)
            self.cb2.SetToolTipString(wx.EmptyString)
        #----------------------------------------------------------------------
        def onToggle1(self, event):
            if self.cb1.GetValue() == True:
                self.cb1.SetToolTipString("Check Box 1 is Checked")
            else:
                self.cb1.SetToolTip(None)
    
        def onToggle2(self, event):
            if self.cb2.GetValue() == True:
                self.cb2.SetToolTipString("Check Box 2 Is Checked")
            else:
                self.cb2.SetToolTip(None)
    
    # Run the program
    if __name__ == "__main__":
        app = wx.App(False)
        frame = MyForm()
        frame.Show()
        app.MainLoop()
    

    Edit: For the code in your question in relation to use in a grid, change the

    if col in relevanCols:
        event.GetEventObject().SetToolTipString("Hai")
    else:
        self.grid.SetToolTip(None)
    

    to:

    if col in relevanCols:
        event.GetEventObject().SetToolTipString("Hai")
    else:
        event.GetEventObject().SetToolTip(None)