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.

(info by ZetCode / Jan Bodnar).


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

   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 
  14 """
  15 
  16 import wx
  17 
  18 # class MyFrame
  19 # class MyApp
  20 
  21 #---------------------------------------------------------------------------
  22 
  23 class MyFrame(wx.Frame):
  24     def __init__(self, parent, id, title):
  25         wx.Frame.__init__(self, parent, id, title,
  26                           wx.DefaultPosition, wx.Size(350, 250))
  27 
  28         self.SetIcon(wx.Icon('./icons/icon_wxWidgets.ico', wx.BITMAP_TYPE_ICO))
  29         self.SetBackgroundColour("gray")
  30 
  31         #------------
  32 
  33         toolbar = wx.ToolBar(self, -1, style=wx.TB_HORIZONTAL | wx.NO_BORDER)
  34         toolbar.AddTool(1, "New", wx.Image('./bitmaps/new.png',
  35                         wx.BITMAP_TYPE_PNG).ConvertToBitmap(),
  36                         wx.NullBitmap, wx.ITEM_NORMAL, 'New', "Long help for 'New'.", None)
  37         toolbar.AddTool(2, "Open", wx.Image('./bitmaps/open.png',
  38                         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',
  41                         wx.BITMAP_TYPE_PNG).ConvertToBitmap(),
  42                         wx.NullBitmap, wx.ITEM_NORMAL, 'Save', "Long help for 'Save'.", None)
  43         toolbar.AddSeparator()
  44         toolbar.AddTool(4, "Exit", wx.Image('./bitmaps/exit.png',
  45                         wx.BITMAP_TYPE_PNG).ConvertToBitmap(),
  46                         wx.NullBitmap, wx.ITEM_NORMAL, 'Exit', "Long help for 'Exit'.", None)
  47         toolbar.Realize()
  48 
  49         #------------
  50 
  51         vbox = wx.BoxSizer(wx.VERTICAL)
  52 
  53         vbox.Add(toolbar, 0, border=5)
  54 
  55         self.SetSizer(vbox)
  56 
  57         #------------
  58 
  59         self.statusbar = self.CreateStatusBar()
  60 
  61         #------------
  62 
  63         self.Bind(wx.EVT_TOOL, self.OnNew, id=1)
  64         self.Bind(wx.EVT_TOOL, self.OnOpen, id=2)
  65         self.Bind(wx.EVT_TOOL, self.OnSave, id=3)
  66         self.Bind(wx.EVT_TOOL, self.OnExit, id=4)
  67 
  68         #------------
  69 
  70         self.Centre()
  71 
  72     #-----------------------------------------------------------------------
  73 
  74     def OnNew(self, event):
  75         self.statusbar.SetStatusText('New Command.')
  76 
  77 
  78     def OnOpen(self, event):
  79         self.statusbar.SetStatusText('Open Command.')
  80 
  81 
  82     def OnSave(self, event):
  83         self.statusbar.SetStatusText('Save Command.')
  84 
  85 
  86     def OnExit(self, event):
  87         self.Close()
  88 
  89 #---------------------------------------------------------------------------
  90 
  91 class MyApp(wx.App):
  92     def OnInit(self):
  93         frame = MyFrame(None, -1, 'wx.Toolbar')
  94         frame.Show(True)
  95 
  96         return True
  97 
  98 #---------------------------------------------------------------------------
  99 
 100 app = MyApp(0)
 101 app.MainLoop()


Sample two

img_sample_two.png

   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 
  13 """
  14 
  15 import wx
  16 
  17 # class MyFrame
  18 # def main
  19 
  20 #---------------------------------------------------------------------------
  21 
  22 class MyFrame(wx.Frame):
  23     def __init__(self, *args, **kwargs):
  24         super(MyFrame, self).__init__(*args, **kwargs)
  25 
  26         self.InitUI()
  27 
  28     #-----------------------------------------------------------------------
  29 
  30     def InitUI(self):
  31         toolbar = self.CreateToolBar()
  32         qtool = toolbar.AddTool(wx.ID_ANY, 'Quit', wx.Bitmap('./bitmaps/exit.png'))
  33         toolbar.Realize()
  34 
  35         self.Bind(wx.EVT_TOOL, self.OnQuit, qtool)
  36 
  37         self.SetIcon(wx.Icon('./icons/icon_wxWidgets.ico', wx.BITMAP_TYPE_ICO))
  38         self.SetSize((350, 250))
  39         self.SetTitle('Simple toolbar')
  40 
  41         self.Centre()
  42 
  43 
  44     def OnQuit(self, event):
  45         self.Close()
  46 
  47 #---------------------------------------------------------------------------
  48 
  49 def main():
  50 
  51     app = wx.App()
  52     ex = MyFrame(None)
  53     ex.Show()
  54     app.MainLoop()
  55 
  56 #---------------------------------------------------------------------------
  57 
  58 if __name__ == '__main__':
  59     main()


Sample three

img_sample_three.png

   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 
  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         vbox = wx.BoxSizer(wx.VERTICAL)
  33 
  34         toolbar1 = wx.ToolBar(self)
  35         toolbar1.AddTool(wx.ID_ANY, '', wx.Bitmap('./bitmaps/new.png'))
  36         toolbar1.AddTool(wx.ID_ANY, '', wx.Bitmap('./bitmaps/open.png'))
  37         toolbar1.AddTool(wx.ID_ANY, '', wx.Bitmap('./bitmaps/save.png'))
  38         toolbar1.Realize()
  39 
  40         toolbar2 = wx.ToolBar(self)
  41         qtool = toolbar2.AddTool(wx.ID_EXIT, '', wx.Bitmap('./bitmaps/exit.png'))
  42         toolbar2.Realize()
  43 
  44         vbox.Add(toolbar1, 0, wx.EXPAND)
  45         vbox.Add(toolbar2, 0, wx.EXPAND)
  46 
  47         self.Bind(wx.EVT_TOOL, self.OnQuit, qtool)
  48 
  49         self.SetSizer(vbox)
  50 
  51         self.SetIcon(wx.Icon('./icons/icon_wxWidgets.ico', wx.BITMAP_TYPE_ICO))
  52         self.SetSize((350, 250))
  53         self.SetTitle('Two toolbars')
  54 
  55         self.Centre()
  56 
  57 
  58     def OnQuit(self, event):
  59         self.Close()
  60 
  61 #---------------------------------------------------------------------------
  62 
  63 def main():
  64     app = wx.App()
  65     ex = MyFrame(None)
  66     ex.Show()
  67     app.MainLoop()
  68 
  69 #---------------------------------------------------------------------------
  70 
  71 if __name__ == '__main__':
  72     main()


Sample four

img_sample_four.png

   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 
  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         self.count = 5
  34 
  35         self.toolbar = self.CreateToolBar()
  36         tundo = self.toolbar.AddTool(wx.ID_UNDO, '', wx.Bitmap('./bitmaps/undo.png'))
  37         tredo = self.toolbar.AddTool(wx.ID_REDO, '', wx.Bitmap('./bitmaps/redo.png'))
  38         self.toolbar.EnableTool(wx.ID_REDO, False)
  39         self.toolbar.AddSeparator()
  40         texit = self.toolbar.AddTool(wx.ID_EXIT, '', wx.Bitmap('./bitmaps/exit.png'))
  41         self.toolbar.Realize()
  42 
  43         self.Bind(wx.EVT_TOOL, self.OnQuit, texit)
  44         self.Bind(wx.EVT_TOOL, self.OnUndo, tundo)
  45         self.Bind(wx.EVT_TOOL, self.OnRedo, tredo)
  46 
  47         self.SetIcon(wx.Icon('./icons/icon_wxWidgets.ico', wx.BITMAP_TYPE_ICO))
  48         self.SetSize((380, 250))
  49         self.SetTitle('Enable / disable toolbar buttons')
  50 
  51         self.Centre()
  52 
  53 
  54     def OnUndo(self, event):
  55         if self.count > 1 and self.count <= 5:
  56             self.count = self.count - 1
  57 
  58 
  59         if self.count == 1:
  60             self.toolbar.EnableTool(wx.ID_UNDO, False)
  61 
  62         if self.count == 4:
  63             self.toolbar.EnableTool(wx.ID_REDO, True)
  64 
  65 
  66     def OnRedo(self, event):
  67         if self.count < 5 and self.count >= 1:
  68             self.count = self.count + 1
  69 
  70         if self.count == 5:
  71             self.toolbar.EnableTool(wx.ID_REDO, False)
  72 
  73         if self.count == 2:
  74             self.toolbar.EnableTool(wx.ID_UNDO, True)
  75 
  76 
  77     def OnQuit(self, event):
  78         self.Close()
  79 
  80 #---------------------------------------------------------------------------
  81 
  82 def main():
  83 
  84     app = wx.App()
  85     ex = MyFrame(None)
  86     ex.Show()
  87     app.MainLoop()
  88 
  89 #---------------------------------------------------------------------------
  90 
  91 if __name__ == '__main__':
  92     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....

How to create a tool bar (Phoenix) (last edited 2020-12-31 16:22:59 by Ecco)

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