凌的博客

您现在的位置是: 首页 > 学无止境 > python > 

python

6. PyQt6 子菜单

2023-10-17 python 304
import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QMenu
from PyQt6.QtGui import QGuiApplication, QAction


class App(QMainWindow):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle("PyQt6 子菜单")

        menubar = self.menuBar()
        file_menu = menubar.addMenu("文件")
        imp_menu = QMenu("导入", self)
        imp_act = QAction("导入邮件", self)
        new_act = QAction("新建", self, checkable=True)
        new_act.setChecked(True)

        new_act.triggered.connect(self.toggleMenu)

        imp_menu.addAction(imp_act)

        file_menu.addAction(new_act)
        file_menu.addMenu(imp_menu)

        self.setGeometry(100, 100, 400, 300)
        self.center()
        
    def toggleMenu(self, state):

        if state:
            self.statusbar.show()
        else:
            self.statusbar.hide()

    def center(self):
        qr = self.frameGeometry()
        cp = QGuiApplication.primaryScreen().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())


if __name__ == "__main__":
    app = QApplication(sys.argv)
    cls_app = App()
    cls_app.show()
    sys.exit(app.exec())

image.png

文章评论

0条评论