How to Create Multiple Toolbars in wxPython


In the realm of GUI programming, wxPython has emerged as a powerful and versatile library, empowering developers to craft stunning graphical user interfaces with ease. Among the many essential components, toolbars play a vital role in providing users with quick access to various functionalities. In this tutorial, we'll dive into the art of creating multiple toolbars using wxPython. By the end, you'll be equipped with the knowledge to enhance your GUI applications with multiple toolbars, thereby offering an improved user experience.

Installation

wxPython Library for GUI Prototyping

As a wrapper for the C++ library wxWidgets, wxPython allows Python developers to harness the power of this mature and battle-tested framework. It has a native look and feel for various platforms, including Windows, macOS, and Linux. With wxPython, you can design visually appealing and responsive desktop applications with a wide array of widgets, such as buttons, text controls, and, of course, toolbars.

pip install wxPython

Algorithm

  • Import the wxPython library.

  • Create a custom window class that inherits from wx.Frame.

  • Initialize the custom window class by calling the parent class constructor and passing the title of the window as an argument.

  • Create a panel within the frame to hold the widgets.

  • Create a toolbar for the window using CreateToolBar() method.

  • Add three tools to the toolbar using AddTool() method:

    • "Open" with a corresponding icon "icon_open.bmp".

    • "Save" with a corresponding icon "icon_save.bmp".

    • "Highlight" with a corresponding icon "icon_highlight.bmp" (a toggle button).

  • Add a ComboBox (dropdown) as Tool 4 to the toolbar with the AddControl() method with choices "Selection 1" and "Selection 2".

  • Initialize the toolbar to display it.

  • Center the window on the screen using the Centre() method.

  • Show the custom window using Show() method.

  • Create the wxPython application using wx.App().

  • Create and show the custom window object.

  • Run the main event loop so that the GUI pops on the screen.

Example

Download and save these icons in the same file as the script otherwise you will face an error.

icon_save.bmp

icon_open.bmp

icon_highlight.bmp

import wx

class CustomWindow(wx.Frame):
   def __init__(self, title):
      # Initialize the parent class constructor
      super().__init__(parent=None, title=title)

      self.panel = wx.Panel(self)

      # Toolbar for the window
      toolbar = self.CreateToolBar()

      # Tool 1 
      open_tool_id = wx.ID_ANY
      open_tool_label = "Open"
      open_tool_bitmap = wx.Bitmap("icon_open.bmp")
      toolbar.AddTool(open_tool_id, open_tool_label, open_tool_bitmap)

      # Tool 2
      save_tool_id = wx.ID_ANY
      save_tool_label = "Save"
      save_tool_bitmap = wx.Bitmap("icon_save.bmp")
      toolbar.AddTool(save_tool_id, save_tool_label, save_tool_bitmap)

      # Tool 3
      highlight_tool_id = wx.ID_ANY
      highlight_tool_label = "Highlight"
      highlight_tool_bitmap = wx.Bitmap("icon_highlight.bmp")
      width, height = 24, 24
      toolbar.AddCheckTool(highlight_tool_id, highlight_tool_label, highlight_tool_bitmap)

      # Combo Box (Dropdown) toolbar
      combo = wx.ComboBox(toolbar, choices=["Selection 1", "Selection 2"])
      toolbar.AddControl(combo)
      toolbar.Realize()
      self.Centre()
      self.Show()

app = wx.App()

# Show the custom window
custom_window = CustomWindow("TutorialsPoint wxPython MultiToolbar")
app.MainLoop()

Output

Explanation

  • First, the wxPython library is imported to use its functionalities.

  • A custom window class named CustomWindow is defined, inheriting from the wx.Frame class.

  • Inside the __init__ constructor of the CustomWindow class:

  • The super().__init__(parent=None, title=title) line initializes the parent class constructor (wx.Frame) with the given title, creating the main window.

  • A panel named self. The panel is created as a child of the frame. Panels are used to hold widgets (controls) in wxPython applications.

  • A toolbar is created for the window using the CreateToolBar() method.

  • Three tools are added to the toolbar using the AddTool() method:

    • Tool 1 has the "Open" label and the "icon open.bmp" icon.

    • The second tool has the "Save" label and the "icon save.bmp" icon.

    • Tool 3's "Highlight" label and "icon highlight.bmp" icon It is designed to be an auditable tool (a toggle button).

  • Using the AddControl() method, a combo box (dropdown) is added as Tool 4 to the menu bar. "Selection 1" and "Selection 2" are the options that are present in the combo box.

Applications

To build complex applications with a variety of functionalities need toolbars. Sometimes one toolbar does not suffice. Separation of features into multiple toolbars streamlines the user experience. It partitions the backend logic and makes the app easy to use and navigate. The same holds true for a variety of productivity tools (such as text editors, music players, and others). Eg. MS Word, Excel, Jira, Music Players, etc have multiple toolbars. Each has a dropdown that contains options related to that particular toolbar.

Conclusion

This tutorial demonstrated how to build many toolbars in wxPython. With the presented code, you enhance GUI application usability. By following the installation procedure and understanding the syntax, you can integrate these toolbars into your projects. Separating toolbars for different functionalities enhances usability and the user experience. wxPython is a reliable companion for creating complex and appealing GUI applications.

Updated on: 09-Aug-2023

62 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements