from PyQt5.QtGui import * from PyQt5.QtWidgets import * class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): exitAct = QAction(QIcon('exit.png'), '&Exit', self) exitAct.setShortcut('Ctrl+Q') exitAct.setStatusTip('Exit application') exitAct.triggered.connect(qApp.quit) menubar = QMenuBar(self) fileMenu = menubar.addMenu('&File') fileMenu.addAction(exitAct) self.setGeometry(300, 300, 300, 200) self.setWindowTitle('Menu Bar') self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())
from PyQt5.QtGui import * from PyQt5.QtWidgets import * class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): openAct = QAction(QIcon('open.png'), 'Open', self) openAct.setStatusTip('Open File') openAct.triggered.connect(self.showDialog) toolbar = QToolBar(self) self.addToolBar(toolbar) toolbar.addAction(openAct) self.setGeometry(300, 300, 300, 200) self.setWindowTitle('Toolbar') self.show() def showDialog(self): QMessageBox.information(self, 'Message', 'Open file clicked') if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())In both examples, PyQt5.QtGui QAction was used. It is part of the PyQt5.QtGui package library.