python - pyqt5中的多个窗口

标签 python pyqt pyqt5

该算法允许每次单击按钮时打开一个新窗口,但是当再次单击该按钮时窗口会关闭,我需要修改它,以便每次单击它都会生成一个新窗口而不关闭上一个。

代码:

import sys
from random import randint

from PyQt5.QtWidgets import (
    QApplication,
    QLabel,
    QMainWindow,
    QPushButton,
    QVBoxLayout,
    QWidget,
)


class AnotherWindow(QWidget):
    """
    This "window" is a QWidget. If it has no parent,
    it will appear as a free-floating window.
    """

    def __init__(self):
        super().__init__()
        layout = QVBoxLayout()
        self.label = QLabel("Another Window % d" % randint(0, 100))
        layout.addWidget(self.label)
        self.setLayout(layout)


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.window1 = AnotherWindow()
        self.window2 = AnotherWindow()

        l = QVBoxLayout()
        button1 = QPushButton("Push for Window 1")
        button1.clicked.connect(self.toggle_window1)
        l.addWidget(button1)

        button2 = QPushButton("Push for Window 2")
        button2.clicked.connect(self.toggle_window2)
        l.addWidget(button2)

        w = QWidget()
        w.setLayout(l)
        self.setCentralWidget(w)

    def toggle_window1(self, checked):
        if self.window1.isVisible():
            self.window1.hide()

        else:
            self.window1.show()

    def toggle_window2(self, checked):
        if self.window2.isVisible():
            self.window2.hide()

        else:
            self.window2.show()


app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()

每次给出按钮时,我都尝试更改新小部件的 Windows 变量,但它不起作用

最佳答案

您的代码正在生成三个窗口,并使用主窗口中的按钮隐藏/显示其他两个窗口。要在按下按钮时生成新窗口,您需要调用 AnotherWindow 的新实例并将它们存储在 MainWindow 中。

例如:

import sys
from random import randint

from PyQt5.QtWidgets import (
    QApplication,
    QLabel,
    QMainWindow,
    QPushButton,
    QVBoxLayout,
    QWidget,
)


class AnotherWindow(QWidget):
    """
    This "window" is a QWidget. If it has no parent,
    it will appear as a free-floating window.
    """

    def __init__(self):
        super().__init__()
        layout = QVBoxLayout()
        self.label = QLabel("Another Window % d" % randint(0, 100))
        layout.addWidget(self.label)
        self.setLayout(layout)


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.windows = []

        l = QVBoxLayout()
        button1 = QPushButton("Push for new window")
        button1.clicked.connect(self.open_newWindow)
        l.addWidget(button1)

        w = QWidget()
        w.setLayout(l)
        self.setCentralWidget(w)

    def open_newWindow(self):
        window = AnotherWindow()
        self.windows.append(window)
        window.show()


app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()

关于python - pyqt5中的多个窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66946885/

相关文章:

python - 使用颜色条从 2d 函数创建矢量图形(svg、eps、pdf) "heatmap"

python - 为什么 Automator 应用程序在作为 LaunchDaemon 运行时会出现权限错误?

python - 如果我们执行符号链接(symbolic link) python 脚本,我们是否可以打印真实文件的路径?

Python PyQt : How Run While Loop Without Locking Main Dialog Window

python - 为什么 PyQt5 中的 Qt.AlignCenter onQLabel 对于图像和文本不同?

python - `@pyqtSlot()` 对嵌套函数有同样的影响吗?

python - 在 OS-X Lion 上安装 Graphite。如何配置apache2?

python - 使用 QT 示例的 PyQt5.Qt3DCore 中的回调机制有什么问题

python - 如何检查是否按下了键盘修饰符(Shift、Ctrl 或 Alt)?

python - 如何让QTabWidget看起来透明?