PyQt5最全34 菜单之menuBar创建和使用菜单
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class Menu(QMainWindow):
"""
创建和使用菜单
menuBar
addMenu/addAction
"""
def __init__(self):
super(Menu, self).__init__()
self.resize(400, 300)
bar = self.menuBar() # 获取菜单栏
file = bar.addMenu('文件')
file.addAction('新建')
save = QAction('保存', self)
save.setShortcut('Ctrl + s')
file.addAction(save)
edit = bar.addMenu('Edit')
edit.addAction('copy')
edit.addAction('paste')
quit = QAction('退出', self)
file.addAction(quit)
save.triggered.connect(self.process)
def process(self):
print(self.sender().text())
if __name__ == '__main__':
app = QApplication(sys.argv)
main = Menu()
print(main.__doc__)
main.show()
sys.exit(app.exec_())
结果
点击保存会有对应的槽
创建和使用菜单
menuBar
保存