0

标题说了什么。我正在研究 PyQt6,当用户单击菜单栏菜单之一中的项目时,我想显示类似关于屏幕的内容。菜单显示正常,但未发生触发事件。有什么我做错了吗?

import sys, os
from PyQt6.QtCore import *
from PyQt6.QtWidgets import *
from PyQt6.QtGui import *


class AnotherWindow(QWidget):
    """
    This "window" is a QWidget. If it has no parent, it
    will appear as a free-floating window as we want.
    """
    def __init__(self):
        super().__init__()
        layout = QVBoxLayout()
        self.label = QLabel("Another Window")
        layout.addWidget(self.label)
        self.setLayout(layout)


class MainWindow(QMainWindow):

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

        self.setFixedSize(300, 300)
        self.setWindowTitle('Try')

class Menu:

    def __init__(self, MainWindow):

        super().__init__()

        self.view = MainWindow

        self.menuBar = QMenuBar()
        self.menuBar.setGeometry(QRect(0, 0, 277, 22))
        self.view.setMenuBar(self.menuBar)
        self.open = QMenu(self.menuBar)
        self.open.setTitle('Open')
        self.menuBar.addAction(self.open.menuAction())
        self.this = QAction(self.menuBar)
        self.this.setText('This')
        self.open.addAction(self.this)
        
        self.this.triggered.connect(self.show_new_window)

    def show_new_window(self, checked):
        self.w = AnotherWindow()
        self.w.show()

app = QApplication(sys.argv)
w = MainWindow()
Menu(w)
w.show()
app.exec()

另外,我想为菜单栏创建一个不同的类以获得更简洁的代码。我实际上需要为我的 GUI 项目执行此操作。如果这会以某种方式破坏代码,请告诉我。非常感谢!

4

0 回答 0