import sys from PyQt5.QtWidgets import QApplication,QMainWindow,QMenu,QMenuBar,QAction class menubarDemo(QMainWindow): def __init__(self): super(menubarDemo, self).__init__() #添加menuBar的3个步骤: 1: addMenu 2.初始化Action 3.给Menu添加Action #创建Menu: File以及给File添加Action: New,Open,Close,Exit #step1 self.menuFile = self.menuBar().addMenu("&File") #step2 self.new = QAction("New") #创建一个Action名为New,没有快捷键 #step3 self.menuFile.addAction(self.new) #同理给Menu:File添加其他Actions #Open self.open = QAction("&Open") self.open.setShortcut("ctrl+O") self.menuFile.addAction(self.open) #Close self.close = QAction("Close") self.menuFile.addAction(self.close) #exit self.exit = QAction("Exit") self.menuFile.addAction(self.exit) #创建Menu: Edit,其Actions: Copy,Past,Cut self.menuEdit = self.menuBar().addMenu("&Edit") if __name__ == '__main__': app = QApplication(sys.argv) demo = menubarDemo() demo.show() sys.exit(app.exec_())
参考:PyQt5基本控件详解之QMenuBar(二十四)_jia666666的博客-CSDN博客_pyqt5 qmenubar