效果如下:
代码如下:
1 #!/usr/bin/python3 2 # -*- coding: utf-8 -*- 3 4 """ 5 This program creates a toolbar. 6 The toolbar has one action, which 7 terminates the application, if triggered. 8 9 """ 10 11 import sys 12 from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication 13 from PyQt5.QtGui import QIcon 14 15 16 class Example(QMainWindow): 17 18 def __init__(self): 19 super().__init__() 20 21 self.initUI() 22 23 def initUI(self): 24 25 exitAct = QAction(QIcon('picturesexit24.png'), 'Exit', self) 26 exitAct.setShortcut('Ctrl+Q') 27 exitAct.triggered.connect(qApp.quit) 28 29 self.toolbar = self.addToolBar('Exit') 30 self.toolbar.addAction(exitAct) 31 32 self.setGeometry(300, 300, 300, 200) 33 self.setWindowTitle('Toolbar') 34 self.show() 35 36 37 if __name__ == '__main__': 38 39 app = QApplication(sys.argv) 40 ex = Example() 41 sys.exit(app.exec_())