菜单栏设置
from PyQt5.Qt import *
import sys
app = QApplication(sys.argv)
win = QWidget()
win.setWindowTitle("菜单栏设置")
win.resize(500,500)
btn = QPushButton(win)
btn.setParent(win)
btn.setText("文件")
# btn.setIcon(QIcon("open.png"))
menu = QMenu()
# 子菜单:最近打开
open_recent_menu = QMenu(menu)
open_recent_menu.setTitle("最近打开")
file_acttion = QAction("img.jpg")
open_recent_menu.addAction(file_acttion)
# 行为:打开 分割线 退出
# 打开
open_action = QAction(QIcon("open.png"),"打开",menu)
open_action.triggered.connect(lambda:print("打开"))
# 退出
exit_action = QAction(QIcon("close.ico"),'退出',menu)
exit_action.triggered.connect(lambda:print("退出"))
menu.addAction(open_action)
menu.addMenu(open_recent_menu)
menu.addSeparator()
menu.addAction(exit_action)
btn.setMenu(menu)
win.show()
sys.exit(app.exec_())
结果: