1 # -*- coding: utf-8 -*- 2 # Created by PCITZDF on 2018/4/8 15:36. 3 # FileName: menuandtools.py 4 5 import sys 6 7 from PyQt5.QtGui import QIcon 8 from PyQt5.QtWidgets import QMainWindow, QApplication, QAction, qApp, QMenu 9 10 11 class Example(QMainWindow): 12 def __init__(self): 13 super().__init__() 14 self.initUI() 15 16 def initUI(self): 17 self.menuInit() 18 self.toolMenuInit() 19 20 self.statusBar().showMessage("Ready") # 設置狀態欄 21 self.setGeometry(300, 300, 250, 150) # 設置窗體位置及大小 22 self.setWindowTitle("Statusbar") # 設置標題 23 self.show() 24 25 def menuInit(self): 26 exitAct = QAction(QIcon('icons/node.png'), "exit", self) 27 exitAct.setShortcut("Ctrl+Q") 28 exitAct.setStatusTip("Exit Application") 29 exitAct.triggered.connect(qApp.quit) 30 31 menubar = self.menuBar() 32 filemenu = menubar.addMenu("File") 33 filemenu.addAction(exitAct) 34 35 impMenu = QMenu("Import", self) 36 37 newActioni = QAction("New", self, checkable=True) 38 impMenu.addAction(newActioni) 39 40 filemenu.addMenu(impMenu) 41 42 def contextMenuEvent(self, e): 43 """右鍵菜單""" 44 cmenu = QMenu(self) 45 46 newAct = cmenu.addAction("new") 47 openAct = cmenu.addAction("Open") 48 quitAct = cmenu.addAction("Quit") 49 action = cmenu.exec_(self.mapToGlobal(e.pos())) 50 if action == quitAct: 51 qApp.quit() 52 53 def toolMenuInit(self): 54 """工具欄菜單""" 55 exitAct = QAction(QIcon('icons/node.png'), "exit", self) 56 exitAct.setShortcut("Ctrl+Q") 57 exitAct.triggered.connect(qApp.quit) 58 exitAct.setToolTip("退出應用") 59 60 self.toolbar = self.addToolBar("Exit") 61 self.toolbar.addAction(exitAct) 62 63 64 65 if __name__ == '__main__': 66 app = QApplication(sys.argv) 67 ex = Example() 68 sys.exit(app.exec_())