How to Create QToolBar in PyQt5

In this PyQt5 article i want to show How to Create QToolBar in PyQt5. the QToolBar class provides a movable panel that contains a set of controls. toolbar buttons are added by adding actions, using addAction() or insertAction(). Groups of buttons can be separated using addSeparator() or insertSeparator().

 

 

What is PyQt5 QToolBar ?

PyQt5 QToolBar is a customizable toolbar widget that provides easy way to group and organize a set of frequently used actions in a PyQt5 application. it is standard widget that is part of the PyQt5 library and can be easily added to PyQt5 applications.

QToolBar is designed to work in conjunction with other PyQt5 classes such as QAction, which represents an action that the user can take in the application. you can add QAction objects to a QToolBar to create toolbar that contains buttons or other widgets that represent the actions.

QToolBar provides several customization options, such as ability to add separators between actions and the ability to add custom widgets to the toolbar. you can also customize the appearance of the toolbar by setting the background color, icon size and other properties.

using QToolBar in your PyQt5 application can help make your application more user friendly by providing easy access to frequently used actions. you can group related actions together on a toolbar, which can make it easier for users to find the actions they need.

To use QToolBar in your PyQt5 application, you can create an instance of the QToolBar class, add QAction objects to it, and then add the toolbar to your application’s main window using the QMainWindow class’s addToolBar() method.

 

 

Basically we are using some codes from the previews example in this post.

 

 

 

Also you can read more Python GUI articles in the below links

 

 

 

 

So now first we need some imports.

 

 

 

After that we create our Window class that extends from QMainWindow and in that class we add some requirements of our window, also we create a method of CreateMenu() in the class . basically this method is for creating menus and toolbar.

 

 

 

So in here we have created some menu items.

 

 

 

 

Also every PyQt5 application must create an application object. The sys.argv parameter is a list of arguments from a command line.

 

 

 

Finally, we enter the mainloop of the application. The event handling starts from this point. 

 

 

 

 

Run the complete code and this will be the result.

How to Create QToolBar in PyQt5
How to Create QToolBar in PyQt5

 

 

 

Complete source code for How to Create QToolBar in PyQt5

 

 

 

Styles and Themes in PyQt5 Toolbar

Also you can add styles to your toolbar in PyQt5, for that you can add this code.

In this example, we’ve added a custom stylesheet to the toolbar using the setStyleSheet() method. stylesheet sets the background color to a dark blue (#34495E), the text color to white (#FFFFFF), and adds a border with a slightly darker shade of blue (#2C3E50).

 

 

 

Add Custom Widgets in PyQt5 Toolbars

Also you can  include custom widgets like combo boxes or line edits within the toolbar

In this example, we’ve added a combo box and a line edit widget to the toolbar using QComboBox and QLineEdit classes respectively. We then wrap these widgets with QWidgetAction instances and add them to the toolbar using addAction() method.

 

 

Now after adding the custom widget and style, this will be the result

How to Create QToolBar in PyQt5
How to Create QToolBar in PyQt5

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×