我正在尝试使用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()
一旦运行for循环,初始化的所有代码都将被执行,其中包括在
initPopup()
末尾使用的show()
调用。你知道吗一个简单的解决方案是创建一个新的信号,该信号在隐藏窗口时发出,并将该信号连接到创建新信号的函数,直到达到最大值。你知道吗
main.py:
alert.py:
只需注意,在这个非常简化的示例中,用户可能会单击“SG”小部件的按钮,即使“alert”窗口是可见的。您可能更喜欢使用QDialog而不是QMainWindow,并使主小部件成为该对话框的父级。你知道吗
main.py:
alert.py:
此外,如果使用“X”按钮关闭警报窗口,则不会自动显示新窗口。为了避免这种情况,可以实现“closeEvent”并忽略该事件,这样用户在单击按钮之前将无法关闭窗口。由于QDialogs可以在按下escape键时自动关闭,我也忽略了这种情况。你知道吗
alert.py:
相关问题 更多 >
编程相关推荐