How to create a tool bar (Phoenix)

Keywords : Task bar icon.


Introduction :

Toolbar is a widget that groups the most common used commands or actions of your application. Typically save, open, cut, copy, paste, undo, redo etc. Its purpose is to save time. You need one click to do an action from the toolbar and two clicks from the menu.

Toolbar widget is created in three steps.

Firstly, we create a toolbar object.

toolbar = wx.ToolBar(self, -1, style=wx.TB_HORIZONTAL | wx.NO_BORDER)

Then we add some tools to the toolbar with the AddSimpleTool() method. You don't find this method in the reference book. It is a wxPython 'extension'. This is a curse and also a blessing. It makes python programming easier. But on the other hand, these extensions are undocumented. You have to look at the wrapper code, demo example or ask on the mailing list.

toolbar.AddTool(1, "Save", wx.Image('stock_new.png',  wx.BITMAP_TYPE_PNG).ConvertToBitmap(), wx.NullBitmap, wx.ITEM_NORMAL, 'Save', "Long help for 'Save'", None)

In the end, we call the Realize() method. This method shows or renders the toolbar widget.

toolbar.Realize()

The toolbar widget has several event handlers. When you click on a toolbar icon a wx.EVT_COMMAND_TOOL_CLICKED event is generated. We bind this event to a specified method with the wx.EVT_TOOL handler.

In order to show some meaningful output to our events, we have set up a statusbar.

self.statusbar = self.CreateStatusBar()

This is yet another wxPython extension. So when we click on a toolbar button, a message is displayed on the statusbar. This is done with the SetStatusText() method.


Demonstrating :

Tested py3.x, wx4.x and Win10.

Are you ready to use some samples ? ;)

Test, modify, correct, complete, improve and share your discoveries ! (!)


Sample one

img_sample_one.png

Toggle line numbers
   1 # sample_one.py
   2 
   3 """
   4 
   5 ZetCode wxPython tutorial.
   6 
   7 In this example, a message is
   8 displayed on the statusbar.
   9 
  10 Author : Jan Bodnar
  11 Website : zetcode.com
  12 Last modified : april 2007
  13 Updated : Ecco december 2020
  14 Link : https://wiki.wxpython.org/Another%20tutorial%20(Phoenix)
  15 
  16 """
  17 
  18 import wx
  19 
  20 # class MyFrame
  21 # class MyApp
  22 
  23 #---------------------------------------------------------------------------
  24 
  25 class MyFrame(wx.Frame):
  26     def __init__(self, parent, id, title):
  27         wx.Frame.__init__(self, parent, id, title,
  28                           wx.DefaultPosition, wx.Size(350, 250))
  29 
  30         self.SetIcon(wx.Icon('./icons/icon_wxWidgets.ico', wx.BITMAP_TYPE_ICO))
  31         self.SetBackgroundColour("gray")
  32 
  33         #------------
  34 
  35         toolbar = wx.ToolBar(self, -1, style=wx.TB_HORIZONTAL | wx.NO_BORDER)
  36         toolbar.AddTool(1, "New", wx.Image('./bitmaps/new.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(),
  37                         wx.NullBitmap, wx.ITEM_NORMAL, 'New', "Long help for 'New'.", None)
  38         toolbar.AddTool(2, "Open", wx.Image('./bitmaps/open.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(),
  39                         wx.NullBitmap, wx.ITEM_NORMAL, 'Open', "Long help for 'Open'.", None)
  40         toolbar.AddTool(3, "Save", wx.Image('./bitmaps/save.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(),
  41                         wx.NullBitmap, wx.ITEM_NORMAL, 'Save', "Long help for 'Save'.", None)
  42         toolbar.AddSeparator()
  43         toolbar.AddTool(4, "Exit", wx.Image('./bitmaps/exit.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(),
  44                         wx.NullBitmap, wx.ITEM_NORMAL, 'Exit', "Long help for 'Exit'.", None)
  45         toolbar.Realize()
  46 
  47         #------------
  48         
  49         vbox = wx.BoxSizer(wx.VERTICAL)
  50 
  51         vbox.Add(toolbar, 0, border=5)
  52 
  53         self.SetSizer(vbox)
  54 
  55         #------------
  56         
  57         self.statusbar = self.CreateStatusBar()
  58 
  59         #------------
  60         
  61         self.Bind(wx.EVT_TOOL, self.OnNew, id=1)
  62         self.Bind(wx.EVT_TOOL, self.OnOpen, id=2)
  63         self.Bind(wx.EVT_TOOL, self.OnSave, id=3)
  64         self.Bind(wx.EVT_TOOL, self.OnExit, id=4)
  65 
  66         #------------
  67         
  68         self.Centre()
  69 
  70     #-----------------------------------------------------------------------
  71         
  72     def OnNew(self, event):
  73         self.statusbar.SetStatusText('New Command.')
  74 
  75 
  76     def OnOpen(self, event):
  77         self.statusbar.SetStatusText('Open Command.')
  78 
  79 
  80     def OnSave(self, event):
  81         self.statusbar.SetStatusText('Save Command.')
  82 
  83 
  84     def OnExit(self, event):
  85         self.Close()
  86 
  87 #---------------------------------------------------------------------------
  88         
  89 class MyApp(wx.App):
  90     def OnInit(self):
  91         frame = MyFrame(None, -1, 'wx.Toolbar')
  92         frame.Show(True)
  93         
  94         return True
  95 
  96 #---------------------------------------------------------------------------
  97     
  98 app = MyApp(0)
  99 app.MainLoop()


Sample two

img_sample_two.png

Toggle line numbers
   1 # sample_two.py
   2 
   3 """
   4 
   5 ZetCode wxPython tutorial.
   6 
   7 This example creates a simple toolbar.
   8 
   9 Author : Jan Bodnar
  10 Website : zetcode.com
  11 Last modified : July 2020
  12 Link : http://zetcode.com/wxpython/gdi/
  13 
  14 """
  15 
  16 import wx
  17 
  18 # class MyFrame
  19 # def main
  20 
  21 #---------------------------------------------------------------------------
  22 
  23 class MyFrame(wx.Frame):
  24     def __init__(self, *args, **kwargs):
  25         super(MyFrame, self).__init__(*args, **kwargs)
  26 
  27         self.InitUI()
  28 
  29     #-----------------------------------------------------------------------
  30         
  31     def InitUI(self):
  32         toolbar = self.CreateToolBar()
  33         qtool = toolbar.AddTool(wx.ID_ANY, 'Quit', wx.Bitmap('./bitmaps/exit.png'))
  34         toolbar.Realize()
  35 
  36         self.Bind(wx.EVT_TOOL, self.OnQuit, qtool)
  37 
  38         self.SetIcon(wx.Icon('./icons/icon_wxWidgets.ico', wx.BITMAP_TYPE_ICO))
  39         self.SetSize((350, 250))
  40         self.SetTitle('Simple toolbar')
  41 
  42         self.Centre()
  43 
  44 
  45     def OnQuit(self, event):
  46         self.Close()
  47 
  48 #---------------------------------------------------------------------------
  49         
  50 def main():
  51 
  52     app = wx.App()
  53     ex = MyFrame(None)
  54     ex.Show()
  55     app.MainLoop()
  56 
  57 #---------------------------------------------------------------------------
  58     
  59 if __name__ == '__main__':
  60     main()


Sample three

img_sample_three.png

Toggle line numbers
   1 # sample_three.py
   2 
   3 """
   4 
   5 ZetCode wxPython tutorial.
   6 
   7 In this example, we create two 
   8 horizontal toolbars.
   9 
  10 Author : Jan Bodnar
  11 Website : zetcode.com
  12 Last modified : July 2020
  13 Link : http://zetcode.com/wxpython/gdi/
  14 
  15 """
  16 
  17 import wx
  18 
  19 # class MyFrame
  20 # def main
  21 
  22 #---------------------------------------------------------------------------
  23 
  24 class MyFrame(wx.Frame):
  25     def __init__(self, *args, **kwargs):
  26         super(MyFrame, self).__init__(*args, **kwargs)
  27 
  28         self.InitUI()
  29 
  30     #-----------------------------------------------------------------------
  31 
  32     def InitUI(self):
  33         vbox = wx.BoxSizer(wx.VERTICAL)
  34 
  35         toolbar1 = wx.ToolBar(self)
  36         toolbar1.AddTool(wx.ID_ANY, '', wx.Bitmap('./bitmaps/new.png'))
  37         toolbar1.AddTool(wx.ID_ANY, '', wx.Bitmap('./bitmaps/open.png'))
  38         toolbar1.AddTool(wx.ID_ANY, '', wx.Bitmap('./bitmaps/save.png'))
  39         toolbar1.Realize()
  40 
  41         toolbar2 = wx.ToolBar(self)
  42         qtool = toolbar2.AddTool(wx.ID_EXIT, '', wx.Bitmap('./bitmaps/exit.png'))
  43         toolbar2.Realize()
  44 
  45         vbox.Add(toolbar1, 0, wx.EXPAND)
  46         vbox.Add(toolbar2, 0, wx.EXPAND)
  47 
  48         self.Bind(wx.EVT_TOOL, self.OnQuit, qtool)
  49 
  50         self.SetSizer(vbox)
  51 
  52         self.SetIcon(wx.Icon('./icons/icon_wxWidgets.ico', wx.BITMAP_TYPE_ICO))
  53         self.SetSize((350, 250))
  54         self.SetTitle('Two toolbars')
  55 
  56         self.Centre()
  57 
  58 
  59     def OnQuit(self, event):
  60         self.Close()
  61 
  62 #---------------------------------------------------------------------------
  63         
  64 def main():
  65     app = wx.App()
  66     ex = MyFrame(None)
  67     ex.Show()
  68     app.MainLoop()
  69 
  70 #---------------------------------------------------------------------------
  71     
  72 if __name__ == '__main__':
  73     main()


Sample four

img_sample_four.png

Toggle line numbers
   1 # sample_four.py
   2 
   3 """
   4 
   5 ZetCode wxPython tutorial.
   6 
   7 In this example, how we can enable
   8 and disable toolbar buttons.
   9 We also add a separator line.
  10 
  11 Author : Jan Bodnar
  12 Website : zetcode.com
  13 Last modified : July 2020
  14 Link : http://zetcode.com/wxpython/gdi/
  15 
  16 """
  17 
  18 import wx
  19 
  20 # class MyFrame
  21 # def main
  22 
  23 #---------------------------------------------------------------------------
  24 
  25 class MyFrame(wx.Frame):
  26     def __init__(self, *args, **kwargs):
  27         super(MyFrame, self).__init__(*args, **kwargs)
  28 
  29         self.InitUI()
  30 
  31     #-----------------------------------------------------------------------
  32 
  33     def InitUI(self):
  34         self.count = 5
  35 
  36         self.toolbar = self.CreateToolBar()
  37         tundo = self.toolbar.AddTool(wx.ID_UNDO, '', wx.Bitmap('./bitmaps/undo.png'))
  38         tredo = self.toolbar.AddTool(wx.ID_REDO, '', wx.Bitmap('./bitmaps/redo.png'))
  39         self.toolbar.EnableTool(wx.ID_REDO, False)
  40         self.toolbar.AddSeparator()
  41         texit = self.toolbar.AddTool(wx.ID_EXIT, '', wx.Bitmap('./bitmaps/exit.png'))
  42         self.toolbar.Realize()
  43 
  44         self.Bind(wx.EVT_TOOL, self.OnQuit, texit)
  45         self.Bind(wx.EVT_TOOL, self.OnUndo, tundo)
  46         self.Bind(wx.EVT_TOOL, self.OnRedo, tredo)
  47 
  48         self.SetIcon(wx.Icon('./icons/icon_wxWidgets.ico', wx.BITMAP_TYPE_ICO))
  49         self.SetSize((380, 250))
  50         self.SetTitle('Enable / disable toolbar buttons')
  51 
  52         self.Centre()
  53 
  54 
  55     def OnUndo(self, event):
  56         if self.count > 1 and self.count <= 5:
  57             self.count = self.count - 1
  58 
  59 
  60         if self.count == 1:
  61             self.toolbar.EnableTool(wx.ID_UNDO, False)
  62 
  63         if self.count == 4:
  64             self.toolbar.EnableTool(wx.ID_REDO, True)
  65 
  66 
  67     def OnRedo(self, event):
  68         if self.count < 5 and self.count >= 1:
  69             self.count = self.count + 1
  70 
  71         if self.count == 5:
  72             self.toolbar.EnableTool(wx.ID_REDO, False)
  73 
  74         if self.count == 2:
  75             self.toolbar.EnableTool(wx.ID_UNDO, True)
  76 
  77 
  78     def OnQuit(self, event):
  79         self.Close()
  80 
  81 #---------------------------------------------------------------------------
  82         
  83 def main():
  84 
  85     app = wx.App()
  86     ex = MyFrame(None)
  87     ex.Show()
  88     app.MainLoop()
  89 
  90 #---------------------------------------------------------------------------
  91     
  92 if __name__ == '__main__':
  93     main()


Download source

source.zip


Additional Information

Link :

- - - - -

https://wiki.wxpython.org/TitleIndex

https://docs.wxpython.org/


Thanks to

Jan Bodnar (sample_one / two / three / four.py coding), the wxPython community...


About this page

Date(d/m/y) Person (bot) Comments :

16/12/20 - Ecco (Created page for wxPython Phoenix).


Comments

- blah, blah, blah....

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.