= How to create a tool bar (Phoenix) = '''Keywords :''' Task bar icon. <<TableOfContents>> -------- = 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 == {{attachment:img_sample_one.png}} {{{#!python # sample_one.py """ ZetCode wxPython tutorial. In this example, a message is displayed on the statusbar. Author : Jan Bodnar Website : zetcode.com Last modified : april 2007 """ import wx # class MyFrame # class MyApp #--------------------------------------------------------------------------- class MyFrame(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(350, 250)) self.SetIcon(wx.Icon('./icons/icon_wxWidgets.ico', wx.BITMAP_TYPE_ICO)) self.SetBackgroundColour("gray") #------------ toolbar = wx.ToolBar(self, -1, style=wx.TB_HORIZONTAL | wx.NO_BORDER) toolbar.AddTool(1, "New", wx.Image('./bitmaps/new.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(), wx.NullBitmap, wx.ITEM_NORMAL, 'New', "Long help for 'New'.", None) toolbar.AddTool(2, "Open", wx.Image('./bitmaps/open.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(), wx.NullBitmap, wx.ITEM_NORMAL, 'Open', "Long help for 'Open'.", None) toolbar.AddTool(3, "Save", wx.Image('./bitmaps/save.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(), wx.NullBitmap, wx.ITEM_NORMAL, 'Save', "Long help for 'Save'.", None) toolbar.AddSeparator() toolbar.AddTool(4, "Exit", wx.Image('./bitmaps/exit.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(), wx.NullBitmap, wx.ITEM_NORMAL, 'Exit', "Long help for 'Exit'.", None) toolbar.Realize() #------------ vbox = wx.BoxSizer(wx.VERTICAL) vbox.Add(toolbar, 0, border=5) self.SetSizer(vbox) #------------ self.statusbar = self.CreateStatusBar() #------------ self.Bind(wx.EVT_TOOL, self.OnNew, id=1) self.Bind(wx.EVT_TOOL, self.OnOpen, id=2) self.Bind(wx.EVT_TOOL, self.OnSave, id=3) self.Bind(wx.EVT_TOOL, self.OnExit, id=4) #------------ self.Centre() #----------------------------------------------------------------------- def OnNew(self, event): self.statusbar.SetStatusText('New Command.') def OnOpen(self, event): self.statusbar.SetStatusText('Open Command.') def OnSave(self, event): self.statusbar.SetStatusText('Save Command.') def OnExit(self, event): self.Close() #--------------------------------------------------------------------------- class MyApp(wx.App): def OnInit(self): frame = MyFrame(None, -1, 'wx.Toolbar') frame.Show(True) return True #--------------------------------------------------------------------------- app = MyApp(0) app.MainLoop() }}} -------- == Sample two == {{attachment:img_sample_two.png}} {{{#!python # sample_two.py """ ZetCode wxPython tutorial. This example creates a simple toolbar. Author : Jan Bodnar Website : zetcode.com Last modified : July 2020 """ import wx # class MyFrame # def main #--------------------------------------------------------------------------- class MyFrame(wx.Frame): def __init__(self, *args, **kwargs): super(MyFrame, self).__init__(*args, **kwargs) self.InitUI() #----------------------------------------------------------------------- def InitUI(self): toolbar = self.CreateToolBar() qtool = toolbar.AddTool(wx.ID_ANY, 'Quit', wx.Bitmap('./bitmaps/exit.png')) toolbar.Realize() self.Bind(wx.EVT_TOOL, self.OnQuit, qtool) self.SetIcon(wx.Icon('./icons/icon_wxWidgets.ico', wx.BITMAP_TYPE_ICO)) self.SetSize((350, 250)) self.SetTitle('Simple toolbar') self.Centre() def OnQuit(self, event): self.Close() #--------------------------------------------------------------------------- def main(): app = wx.App() ex = MyFrame(None) ex.Show() app.MainLoop() #--------------------------------------------------------------------------- if __name__ == '__main__': main() }}} -------- == Sample three == {{attachment:img_sample_three.png}} {{{#!python # sample_three.py """ ZetCode wxPython tutorial. In this example, we create two horizontal toolbars. Author : Jan Bodnar Website : zetcode.com Last modified : July 2020 """ import wx # class MyFrame # def main #--------------------------------------------------------------------------- class MyFrame(wx.Frame): def __init__(self, *args, **kwargs): super(MyFrame, self).__init__(*args, **kwargs) self.InitUI() #----------------------------------------------------------------------- def InitUI(self): vbox = wx.BoxSizer(wx.VERTICAL) toolbar1 = wx.ToolBar(self) toolbar1.AddTool(wx.ID_ANY, '', wx.Bitmap('./bitmaps/new.png')) toolbar1.AddTool(wx.ID_ANY, '', wx.Bitmap('./bitmaps/open.png')) toolbar1.AddTool(wx.ID_ANY, '', wx.Bitmap('./bitmaps/save.png')) toolbar1.Realize() toolbar2 = wx.ToolBar(self) qtool = toolbar2.AddTool(wx.ID_EXIT, '', wx.Bitmap('./bitmaps/exit.png')) toolbar2.Realize() vbox.Add(toolbar1, 0, wx.EXPAND) vbox.Add(toolbar2, 0, wx.EXPAND) self.Bind(wx.EVT_TOOL, self.OnQuit, qtool) self.SetSizer(vbox) self.SetIcon(wx.Icon('./icons/icon_wxWidgets.ico', wx.BITMAP_TYPE_ICO)) self.SetSize((350, 250)) self.SetTitle('Two toolbars') self.Centre() def OnQuit(self, event): self.Close() #--------------------------------------------------------------------------- def main(): app = wx.App() ex = MyFrame(None) ex.Show() app.MainLoop() #--------------------------------------------------------------------------- if __name__ == '__main__': main() }}} -------- == Sample four == {{attachment:img_sample_four.png}} {{{#!python # sample_four.py """ ZetCode wxPython tutorial. In this example, how we can enable and disable toolbar buttons. We also add a separator line. Author : Jan Bodnar Website : zetcode.com Last modified : July 2020 """ import wx # class MyFrame # def main #--------------------------------------------------------------------------- class MyFrame(wx.Frame): def __init__(self, *args, **kwargs): super(MyFrame, self).__init__(*args, **kwargs) self.InitUI() #----------------------------------------------------------------------- def InitUI(self): self.count = 5 self.toolbar = self.CreateToolBar() tundo = self.toolbar.AddTool(wx.ID_UNDO, '', wx.Bitmap('./bitmaps/undo.png')) tredo = self.toolbar.AddTool(wx.ID_REDO, '', wx.Bitmap('./bitmaps/redo.png')) self.toolbar.EnableTool(wx.ID_REDO, False) self.toolbar.AddSeparator() texit = self.toolbar.AddTool(wx.ID_EXIT, '', wx.Bitmap('./bitmaps/exit.png')) self.toolbar.Realize() self.Bind(wx.EVT_TOOL, self.OnQuit, texit) self.Bind(wx.EVT_TOOL, self.OnUndo, tundo) self.Bind(wx.EVT_TOOL, self.OnRedo, tredo) self.SetIcon(wx.Icon('./icons/icon_wxWidgets.ico', wx.BITMAP_TYPE_ICO)) self.SetSize((380, 250)) self.SetTitle('Enable / disable toolbar buttons') self.Centre() def OnUndo(self, event): if self.count > 1 and self.count <= 5: self.count = self.count - 1 if self.count == 1: self.toolbar.EnableTool(wx.ID_UNDO, False) if self.count == 4: self.toolbar.EnableTool(wx.ID_REDO, True) def OnRedo(self, event): if self.count < 5 and self.count >= 1: self.count = self.count + 1 if self.count == 5: self.toolbar.EnableTool(wx.ID_REDO, False) if self.count == 2: self.toolbar.EnableTool(wx.ID_UNDO, True) def OnQuit(self, event): self.Close() #--------------------------------------------------------------------------- def main(): app = wx.App() ex = MyFrame(None) ex.Show() app.MainLoop() #--------------------------------------------------------------------------- if __name__ == '__main__': main() }}} -------- = Download source = [[attachment: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....