In PyQt, adding a toolbar to a GUI can be a useful way to provide easy access to commonly used functions or actions. Toolbars typically consist of icons or buttons that represent specific actions, such as saving a file, opening a new document, or printing a document. In this tutorial, we will walk through the process of adding a toolbar to a PyQt application.
Step 1: Import the necessary modules
Before we can add a toolbar to our PyQt application, we need to import the necessary modules. We will need to import both the PyQt5.QtWidgets module to create the toolbar and the PyQt5.QtGui module to display icons on the toolbar. Additionally, we will import the QApplication and QMainWindow classes to create our main application window.
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QToolBar, QAction
from PyQt5.QtGui import QIcon
Step 2: Create the main application window
Next, we need to create an instance of the QMainWindow class to serve as our main application window. We will also set the window title, size, and position.
class MyApplication(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("PyQt Toolbar Example")
self.setGeometry(100, 100, 800, 600)
Step 3: Create the toolbar
Now, we can create the toolbar and add it to our main application window. We will instantiate a QToolBar object and add it to the QMainWindow using the addToolBar() method.
class MyApplication(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("PyQt Toolbar Example")
self.setGeometry(100, 100, 800, 600)
self.toolbar = self.addToolBar('Toolbar')
Step 4: Add actions to the toolbar
To add actions to the toolbar, we will create instances of the QAction class, which represent individual actions. We can specify a name for the action, an icon, a trigger function, and a tooltip. We can then add the action to the toolbar using the addAction() method.
class MyApplication(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("PyQt Toolbar Example")
self.setGeometry(100, 100, 800, 600)
self.toolbar = self.addToolBar('Toolbar')
save_action = QAction(QIcon('save.png'), 'Save', self)
save_action.triggered.connect(self.save)
save_action.setStatusTip('Save File')
self.toolbar.addAction(save_action)
Step 5: Define the trigger function
Finally, we need to define the trigger function that will be called when the action is triggered. This function will perform the specific action associated with the toolbar button.
class MyApplication(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("PyQt Toolbar Example")
self.setGeometry(100, 100, 800, 600)
self.toolbar = self.addToolBar('Toolbar')
save_action = QAction(QIcon('save.png'), 'Save', self)
save_action.triggered.connect(self.save)
save_action.setStatusTip('Save File')
self.toolbar.addAction(save_action)
def save(self):
print("Save file function called")
Step 6: Run the application
To run the application and display the toolbar, we need to create an instance of the QApplication class and run the main event loop.
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyApplication()
window.show()
sys.exit(app.exec_())
With these steps, you should now have a PyQt application with a toolbar that includes a "Save" action. You can expand on this tutorial by adding more actions to the toolbar and defining additional trigger functions for each action. This will allow you to create a fully functional application with a customizable toolbar.