我想在触发 QAction 时显示另一个窗口,但它不起作用

问题描述

标题所说的。我正在研究 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,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 项目执行此操作。如果这会以某种方式破坏代码,请告诉我。非常感谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)