0

I have wxpython toolbar with couple of icons, for example connect, play, save etc. I want to toggle the connect icon.

When I am connected then connect bitmap should replace with disconnect and vice versa. Is there any ready made method available in wxpython to achieve this behaviour in toolbar. The other way I am using is showing both connect and disconnect and enable/disable based on action, but I have so many icons.. so with toggling I want to save some space

1 Answer 1

3

With the regular wx.ToggleButton, you'll want to change the icon in the toggle event handler:

import wx

########################################################################
class MyPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)

        self.save_ico = wx.ArtProvider.GetBitmap(wx.ART_FILE_SAVE, wx.ART_TOOLBAR, (16,16))
        self.print_ico = wx.ArtProvider.GetBitmap(wx.ART_PRINT, wx.ART_TOOLBAR, (16,16))

        self.toggleBtn = wx.ToggleButton(self)
        self.toggleBtn.SetBitmap(self.save_ico)
        self.toggleBtn.Bind(wx.EVT_TOGGLEBUTTON, self.onToggle)

    #----------------------------------------------------------------------
    def onToggle(self, event):
        """"""
        btn = event.GetEventObject()
        if btn.GetValue():
            self.toggleBtn.SetBitmap(self.print_ico)
        else:
            self.toggleBtn.SetBitmap(self.save_ico)


########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Toggle buttons")
        panel = MyPanel(self)
        self.Show()


#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()

If you use the generic version of the ToggleButton, then you can use the SetBitmapSelected() method. You can find the generic version in wx.lib.buttons. You can see an example of this in the wxPython demo package.

Oddly enough, the SetBitmapSelected() appears to be present for the regular ToggleButton starting in wxPython 2.9, however it doesn't function the same way. You only see the alternate image when you are pressing on the button, but it then reverts back when the button is released.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.