Search code examples
pythonwxpythonwxwidgets

How to create an info icon with wxPython


I so far failed to create what is colloquially called an "info icon" with wxPython. An icon with some sort of 'i' image that shows a large tooltip on hover.

I can add a wx.StaticBitmap for the image but it ignores all SetToolTipString or SetToolTip(wx.ToolTip()) calls. OR I can add a large tool tip to a wx.StaticText as shown below.

enter image description here Ignore that the icon doesn't have the correct size yet.

Needless to say that eventually the tooltip needs a background color that is different from the panel background color (not the focus here). I can't use wx.adv.RichToolTip because I'm on wxPython 3.0.2.0 osx-cocoa.

What is a good way to solve this?


Solution

  • If all you want to do is show a tooltip when the image is moused over, then you need to bind your instance of the wx.StaticBitmap to EVT_MOTION:

    import wx
    
    class MyPanel(wx.Panel):
    
        def __init__(self, parent):
            wx.Panel.__init__(self, parent)
    
            bmp = wx.ArtProvider.GetBitmap(wx.ART_WARNING)
            self.image = wx.StaticBitmap(self, bitmap=bmp)
    
            self.image.Bind(wx.EVT_MOTION, self.on_mouse_over)
    
        def on_mouse_over(self, event):
            self.image.SetToolTipString('BLAH BLAH BLAH')
    
    
    class MyFrame(wx.Frame):
    
        def __init__(self):
            wx.Frame.__init__(self, None, title='Icon Mouser')
            panel = MyPanel(self)
            self.Show()
    
    if __name__ == '__main__':
        app = wx.App(False)
        frame = MyFrame()
        app.MainLoop()
    

    When I run this code, I get something like this:

    enter image description here