[PyQt5][原创]菜单栏的使用

纵饮孤独 提交于 2019-12-11 04:04:44
# QMenuBar/QMenu/QAction的使用(菜单栏)
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QDir
from PyQt5.QtGui import QIcon, QPixmap, QFont
from PyQt5.QtCore import QDate

import sys


class MainForm(QMainWindow):

    def __init__(self, parent=None):
        super(MainForm, self).__init__(parent)
        self.setFixedSize(500, 500)
        self.layout = QHBoxLayout()
        self.menubar = self.menuBar()  # 获取窗体的菜单栏

        self.file = self.menubar.addMenu("菜单")
        self.file.addAction("新建")

        self.save = QAction("保存", self)
        self.save.setShortcut("Ctrl+S")  # 设置快捷键
        self.file.addAction(self.save)

        self.edit = self.file.addMenu("方向")
        self.edit.addAction("上")
        self.edit.addAction("下")

        self.quit = QAction("退出", self)
        self.file.addAction(self.quit)
        self.file.triggered[QAction].connect(self.process_trigger)
        self.setLayout(self.layout)
        self.setWindowTitle("菜单的使用")

    def process_trigger(self, qaction):
        print(qaction.text() + "触发")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainForm = MainForm()
    mainForm.show()
    sys.exit(app.exec_())
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!