一、常用方法
二、示例
from PyQt5 import QtWidgets, QtCore, QtGuiclass Demo(QtWidgets.QMainWindow):def __init__(self):super().__init__()self.resize(400, 400)menuBar = self.menuBar()m1 = menuBar.addMenu("打开")action = QtWidgets.QAction(QtGui.QIcon('default.png'), "文件", self)action.triggered.connect(self.open_file)action.setShortcut("Ctrl+O")m1.addAction(action)# 设置分割线 m1.addSeparator()menu = QtWidgets.QMenu(self)menu.setTitle("更多")menu.addAction("打开")m1.addMenu(menu)def open_file(self):path, _ = QtWidgets.QFileDialog.getOpenFileName(None, "选择文件", "", "图片文件(*.png;*.jpg)")if path != "":print(path)else:print('取消')def contextMenuEvent(self, event):menu = QtWidgets.QMenu(self)a1 = QtWidgets.QAction("测试1")a2 = QtWidgets.QAction("测试2")menu.addActions([a1, a2])action = menu.exec_(self.mapToGlobal(event.pos()))if action == a1:print("click 测试1")elif action == a2:print("click 测试2")if __name__ == '__main__':app = QtWidgets.QApplication([])d = Demo()d.show()app.exit(app.exec_())