如何在PyQT5的每个循环中显示新的QMainWindow?

2024-04-19 12:36:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用PyQt5编写一个Python程序,它将在for循环的每个迭代中显示一个窗口。我想在递增并显示下一个窗口后关闭。但是,我不知道如何停止每次迭代的循环,目前我一次得到6个窗口。你知道吗

你知道吗主.py你知道吗

import sys
from PyQt5.QtWidgets import (QLineEdit, QVBoxLayout, QMainWindow, 
    QWidget, QDesktopWidget, QApplication, QPushButton, QLabel, 
    QComboBox, QFileDialog, QRadioButton)
from PyQt5.QtCore import pyqtSlot, QByteArray
from alert import Window2
from test import test

class SG(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.resize(300, 150)
        self.setWindowTitle('TEST')

        self.resultsGen = QPushButton('TEST', self)
        self.resultsGen.clicked.connect(lambda: self.on_click())

        self.show()

    @pyqtSlot()
    def on_click(self):
        test(self)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    sg = SG()

    sys.exit(app.exec_())

你知道吗警报.py你知道吗

from PyQt5.QtWidgets import (QLineEdit, QVBoxLayout, QMainWindow, 
    QWidget, QDesktopWidget, QApplication, QPushButton, QLabel, 
    QComboBox, QFileDialog, QRadioButton)
from PyQt5.QtCore import pyqtSlot, QByteArray
from PyQt5.QtGui import QPixmap

from PyQt5 import QtGui, QtCore

class Window2(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initPopup()

    def initPopup(self):
        self.resize(500, 500)
        self.setWindowTitle("Window22222")
        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)
        lay = QVBoxLayout(self.central_widget)

        label = QLabel(self)
        pixmap = QPixmap('cropped/8.png')
        label.setPixmap(pixmap)
        self.resize(pixmap.width(), pixmap.height())

        lay.addWidget(label)

        self.textbox = QLineEdit(self)
        self.textbox.move(20, 20)
        self.textbox.resize(280, 40)

        # Create a button in the window
        self.button = QPushButton('Show text', self)
        self.button.move(20, 80)
        # connect button to function on_click
        self.button.clicked.connect(lambda: self.on_clickX())
        self.show()

    @pyqtSlot()
    def on_clickX(self):
        textboxValue = self.textbox.text()
        print(textboxValue)
        self.textbox.setText("")
        self.hide()

你知道吗测试.py你知道吗

from alert import Window2

def test(self):
    for x in range(6):
        w = Window2()

Tags: fromtestimportselfinitondefbutton
1条回答
网友
1楼 · 发布于 2024-04-19 12:36:05

一旦运行for循环,初始化的所有代码都将被执行,其中包括在initPopup()末尾使用的show()调用。你知道吗

一个简单的解决方案是创建一个新的信号,该信号在隐藏窗口时发出,并将该信号连接到创建新信号的函数,直到达到最大值。你知道吗

main.py:

import sys
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton
from alert import Window2

class SG(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
        self.alerts = []

    def initUI(self):
        self.resize(300, 150)
        self.setWindowTitle('TEST')

        self.resultsGen = QPushButton('TEST', self)
        self.resultsGen.clicked.connect(self.nextAlert)

        self.show()

    def nextAlert(self):
        if len(self.alerts) >= 6:
            return
        alert = Window2()
        self.alerts.append(alert)
        alert.setWindowTitle('Window {}'.format(len(self.alerts)))
        alert.closed.connect(self.nextAlert)
        alert.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    sg = SG()

    sys.exit(app.exec_())

alert.py:

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class Window2(QMainWindow):
    closed = pyqtSignal()
    def __init__(self):
        super().__init__()
        self.initPopup()

    def initPopup(self):
        self.resize(500, 500)
        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)
        lay = QVBoxLayout(self.central_widget)

        label = QLabel(self)
        pixmap = QPixmap('cropped/8.png')
        label.setPixmap(pixmap)
        self.resize(pixmap.width(), pixmap.height())

        lay.addWidget(label)

        self.textbox = QLineEdit(self)
        lay.addWidget(self.textbox)

        # Create a button in the window
        self.button = QPushButton('Show text', self)
        lay.addWidget(self.button)
        # connect button to function on_click
        self.button.clicked.connect(lambda: self.on_clickX())
        self.show()

    @pyqtSlot()
    def on_clickX(self):
        textboxValue = self.textbox.text()
        print(textboxValue)
        self.textbox.setText("")
        self.hide()
        self.closed.emit()

只需注意,在这个非常简化的示例中,用户可能会单击“SG”小部件的按钮,即使“alert”窗口是可见的。您可能更喜欢使用QDialog而不是QMainWindow,并使主小部件成为该对话框的父级。你知道吗

main.py:

class SG(QWidget):
    # ...
    def nextAlert(self):
        if len(self.alerts) >= 6:
            return
        alert = Window2(self)
        # ...

alert.py:

class Window2(QDialog):
    closed = pyqtSignal()
    def __init__(self, parent):
        super().__init__()
        self.initPopup()

    def initPopup(self):
        self.resize(500, 500)
        # a QDialog doesn't need a central widget
        lay = QVBoxLayout(self)
        # ...

此外,如果使用“X”按钮关闭警报窗口,则不会自动显示新窗口。为了避免这种情况,可以实现“closeEvent”并忽略该事件,这样用户在单击按钮之前将无法关闭窗口。由于QDialogs可以在按下escape键时自动关闭,我也忽略了这种情况。你知道吗

alert.py:

class Window2(QMainWindow):
    # ...
    def closeEvent(self, event):
        event.ignore()

    def keyPressEvent(self, event):
        if event.key() != Qt.Key_Escape:
            super().keyPressEvent(event)

相关问题 更多 >