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.