Search code examples
pythonwxpythontoolbarwxwidgets

Add Custom Art to ToolBar


This source code

class MyToolBar(wx.ToolBar):
  def AddTool2(self, id, shortHelp = '', longHelp = ''):
    global TB_SIZE
    try:
      ArtId = ArtMap.get(id)
      Ico = wx.ArtProvider.GetBitmap(ArtId, wx.ART_TOOLBAR, TB_SIZE)
      self.AddSimpleTool(id, Ico, shortHelp, longHelp)
    except StandardError:
      print('Something wrong, maybe wrong id')

Class MyFrame(wx.Frame):
  def __init__(self, parent, *args, **kwargs):
    wx.Frame.__init__(self, parent, *args, **kwargs)

    ToolBar = MyToolBar(self)
    ToolBar.AddTool2(wx.ID_NEW, 'New', 'Creates new file')
    self.SetToolBar(ToolBar)
    self.GetToolBar().Realize()


ArtMap = { wx.ID_NEW : wx.ART_NEW,
          }
ID_BOUNCE = wx.NewId()
TB_SIZE = wx.Size(16,16)

app = wx.app()
frame = MyFrame(None, -1, 'MyFrame', (0,0))
app.MainLoop()

works well for adding tools to toolbar when the tool has a wx.ART. But how do you add a new tool that have no wx.ART or no wx.ART that can represent it well like the ID_BOUNCE where the tool Bounce is suppose to make a ball bounce in the frame?

Thanks in advance.


Solution

  • wx.ToolBar has AddLabelTool method with a bitmap parameter.
    Find an example over at zetcode.