PyQt: Create a toolbar

If menu is a collection of all the commands used in your application, the toolbar makes it easier to use frequently used commands. (See QToolBar official document)

Store the icons for each function in the toolbar in the folder.

save.png, edit.png, print.png, exit.png

Related course: Create Desktop Apps with Python PyQt5

Create a toolbar.

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, qApp
from PyQt5.QtGui import QIcon

class MyApp(QMainWindow):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        exitAction = QAction(QIcon('exit.png'), 'Exit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.setStatusTip('Exit application')
        exitAction.triggered.connect(qApp.quit)

        self.statusBar()

        self.toolbar = self.addToolBar('Exit')
        self.toolbar.addAction(exitAction)

        self.setWindowTitle('Toolbar')
        self.setGeometry(300, 300, 300, 200)
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MyApp()
    sys.exit(app.exec_())

I created a simple toolbar. The toolbar contains one 'exitAction' that exits the application when selected.

Description

exitAction = QAction(QIcon('exit.png'), 'Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit application')
exitAction.triggered.connect(qApp.quit)

As with menubars, create one QAction object. This object contains icons (.png), labels ('Exit'), and can be executed via shortcuts (Ctrl+Q). The status bar shows the message ('Exit application'), and the signal generated by the click is associated with the quit() method.

self.toolbar = self.addToolBar('Exit')
self.toolbar.addAction(exitAction)

I used addToolbar() to create the toolbar, and I added exitAction behavior to the toolbar using addAction().