Search code examples
pythonuser-interfacewxpythonwxwidgets

wxPython - Setting a help string for a button in toolbar


I noticed I can have a help string appear in the status bar whenever I mouse over tools in my toolbar. I cannot find a way to accomplish this with text buttons.

My toolbar creation is similar to

# Make Tool Bar
toolbar = self.CreateToolBar()
# Make Tool Bar Items
# Play
self.addBasicTool(toolbar, "Play",
                  "This is my help string",
                  stuff.image_play,
                  self.OnPlay)
# My Button
btn = wx.Button(toolbar, wx.ID_OPEN, label="TEXT BUTTON ")
btn.Bind(wx.EVT_BUTTON, self.OnButtonPress)
toolbar.AddControl(btn)

addBasicTool just takes the image, scales it to a proper size, creates the tool with AddBasicTool, and binds the tool to the handler.

def addBasicTool(self, toolbar, label, desc, imgPath, handler):
    icon_width=stuff.toolbar_icon_w
    icon_height=stuff.toolbar_icon_h
    size = (icon_width, icon_height)
    img = wx.Image(imgPath, wx.BITMAP_TYPE_ANY).\
                   Scale(*size).ConvertToBitmap()
    tool = toolbar.AddSimpleTool(-1, img, label, desc)
    self.Bind(wx.EVT_MENU, handler, tool)

For the tool, the helper string is set pretty straight forward. I can't find anything to do the same with a button.

This button may just end up being a filler until I get an icon for it, but I'm still curious how helper strings can be done. I could have a handler that sets the statusBar when the mouse is over the button, but I feel like that is already done somewhere. Thanks the help


Solution

  • Basically you'll have to catch the mouse as it moves over your buttons and update the status bar accordingly. It's not very hard. You just need to bind to wx.EVT_MOTION. Here's a simple example:

    import wx
    
    ########################################################################
    class MyPanel(wx.Panel):
        """"""
    
        #----------------------------------------------------------------------
        def __init__(self, parent):
            """Constructor"""
            wx.Panel.__init__(self, parent)
            self.frame = parent
    
            test_btn = wx.Button(self, label='Test Button')
            test_btn.Bind(wx.EVT_MOTION, self.updateStatusBar)
    
            test_btn_2 = wx.Button(self, label='Test Button')
            test_btn_2.Bind(wx.EVT_MOTION, self.updateStatusBar)
    
            self.buttons = {test_btn: 'Test help string',
                            test_btn_2: 'Another string'}
    
            main_sizer = wx.BoxSizer(wx.VERTICAL)
            main_sizer.Add(test_btn, 0, wx.ALL, 5)
            main_sizer.Add(test_btn_2, 0, wx.ALL, 5)
            self.SetSizer(main_sizer)
    
        #----------------------------------------------------------------------
        def updateStatusBar(self, event):
            """"""
            btn = event.GetEventObject()
            if btn in self.buttons:
                status = self.buttons[btn]
                self.frame.sb.SetStatusText(status)
    
    
    
    ########################################################################
    class MyFrame(wx.Frame):
        """"""
    
        #----------------------------------------------------------------------
        def __init__(self):
            """Constructor"""
            wx.Frame.__init__(self, None, title='Test Help Strings')
            panel = MyPanel(self)
            self.sb = self.CreateStatusBar()
            self.Show()
    
    if __name__ == '__main__':
        app = wx.App(False)
        frame = MyFrame()
        app.MainLoop()