Python PyQt5在循环中的一段时间后在窗口之间切换
我使用下面的代码,在stackoverflow上找到。我想建立一个循环,从主窗口上的一个按钮开始。单击后,window2应打开30秒。然后window3将打开30秒。然后窗口2应该打开,依此类推 我做了一个幻灯片方法,但它不起作用 我尝试使用window2()和window3()以及slide()方法 我的目标是什么 我想要一个有两个交替窗口(window2和window3)的幻灯片。 不是更多,但这是一个无止境的循环Python PyQt5在循环中的一段时间后在窗口之间切换,python,pyqt,Python,Pyqt,我使用下面的代码,在stackoverflow上找到。我想建立一个循环,从主窗口上的一个按钮开始。单击后,window2应打开30秒。然后window3将打开30秒。然后窗口2应该打开,依此类推 我做了一个幻灯片方法,但它不起作用 我尝试使用window2()和window3()以及slide()方法 我的目标是什么 我想要一个有两个交替窗口(window2和window3)的幻灯片。 不是更多,但这是一个无止境的循环 import sys from PyQt5 import QtGui fro
import sys
from PyQt5 import QtGui
from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton,
QToolTip, QMessageBox, QLabel)
class Window2(QMainWindow): # <===
def __init__(self):
super().__init__()
self.setWindowTitle("Window22222")
class Window3(QMainWindow): # <===
def __init__(self):
super().__init__()
self.setWindowTitle("Window333333")
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.title = "First Window"
self.top = 100
self.left = 100
self.width = 680
self.height = 500
self.pushButton = QPushButton("Start", self)
self.pushButton.move(275, 200)
self.pushButton.setToolTip("<h3>Start the Session</h3>")
self.pushButton.clicked.connect(self.window2)
self.pushButton1 = QPushButton("Start", self)
self.pushButton1.move(20, 20)
self.pushButton1.setToolTip("<h3>Start the Session</h3>")
self.pushButton1.clicked.connect(self.slide)
self.main_window()
def main_window(self):
self.label = QLabel("Manager", self)
self.label.move(285, 175)
self.setWindowTitle(self.title)
self.setGeometry(self.top, self.left, self.width, self.height)
self.show()
def window2(self): # <===
self.w2 = Window2()
self.w2.show()
time.sleep(5)
self.w3 = Window3()
self.w3.show()
self.w2.hide()
self.window3()
#self.hide()
def window3(self): # <===
self.w3 = Window3()
self.w3.show()
time.sleep(5)
self.w2 = Window2()
self.w2.show()
self.w3.hide()
self.window2()
#self.hide()
def slide(self):
print ("slide")
self.window2()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
sys.exit(app.exec())
导入系统
从PyQt5导入QtGui
从PyQt5.QtWidgets导入(QApplication、QMainWindow、QPushButton、,
QToolTip、QMessageBox、QLabel)
类窗口2(QMainWindow):#尝试一下:
import sys
#import time
from PyQt5 import QtGui, QtCore
from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton,
QToolTip, QMessageBox, QLabel)
class Window2(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Window 22222")
class Window3(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Window 333333")
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.title = "First Window"
self.top, self.left, self.width, self.height = 100, 100, 680, 500
self.flag = ...
self.pushButton = QPushButton("Start", self)
self.pushButton.move(275, 200)
self.pushButton.setToolTip("<h3>Start the Session</h3>")
self.pushButton.clicked.connect(lambda: self.start_slide(True)) #(self.window2)
self.pushButton1 = QPushButton("Start", self)
self.pushButton1.move(20, 20)
self.pushButton1.setToolTip("<h3>Start the Session</h3>")
self.pushButton1.clicked.connect(lambda: self.start_slide(False))
# +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
self.main_window()
self.w2 = Window2()
self.w3 = Window3()
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.viewWindows)
def start_slide(self, flag):
self.timer.stop()
self.w3.hide()
self.w2.hide()
if flag: self.w2.show()
else: self.w3.show()
self.flag = not flag
self.timer.start(5000)
def viewWindows(self):
if self.flag:
self.w2.show()
self.w3.hide()
else:
self.w3.show()
self.w2.hide()
self.flag = not self.flag
def closeEvent(self, event):
self.w2.close()
self.w3.close()
self.close()
# +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
def main_window(self):
self.label = QLabel("Manager", self)
self.label.move(285, 175)
self.setWindowTitle(self.title)
self.setGeometry(self.top, self.left, self.width, self.height)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
导入系统
#导入时间
从PyQt5导入QtGui、QtCore
从PyQt5.QtWidgets导入(QApplication、QMainWindow、QPushButton、,
QToolTip、QMessageBox、QLabel)
类窗口2(QMainWindow):
定义初始化(自):
super()。\uuuu init\uuuuu()
self.setWindowTitle(“22222窗口”)
类窗口3(QMainWindow):
定义初始化(自):
super()。\uuuu init\uuuuu()
self.setWindowTitle(“窗口333”)
类窗口(QMainWindow):
定义初始化(自):
super()。\uuuu init\uuuuu()
self.title=“第一个窗口”
self.top、self.left、self.width、self.height=100100680500
self.flag=。。。
self.butdown=QPushButton(“启动”,self)
自动按钮移动(275200)
self.butdown.setToolTip(“启动会话”)
self.button.clicked.connect(lambda:self.start_幻灯片(True))35;(self.window2)
self.pushButton1=QPushButton(“开始”,self)
自动按钮1.移动(20,20)
self.pushButton1.setToolTip(“启动会话”)
self.pushButton1.clicked.connect(lambda:self.start\u幻灯片(False))
#++VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
self.main_窗口()
self.w2=Window2()
self.w3=Window3()
self.timer=QtCore.QTimer(self)
self.timer.timeout.connect(self.viewWindows)
def start_幻灯片(自身,标志):
self.timer.stop()
self.w3.hide()
self.w2.hide()
if标志:self.w2.show()
else:self.w3.show()
self.flag=not标志
自动定时器启动(5000)
def viewWindows(自):
如果self.flag:
self.w2.show()
self.w3.hide()
其他:
self.w3.show()
self.w2.hide()
self.flag=非self.flag
def关闭事件(自身、事件):
self.w2.close()
self.w3.close()
self.close()
# +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
def主窗口(自):
self.label=QLabel(“经理”,self)
self.label.move(285175)
self.setWindowTitle(self.title)
self.setGeometry(self.top、self.left、self.width、self.height)
如果名称=“\uuuuu main\uuuuuuuu”:
app=QApplication(sys.argv)
window=window()
window.show()
sys.exit(app.exec_())
您遗漏了导入时间,但除此之外,此代码的工作延迟为5秒,而不是30秒。但是,如果使用
time.sleep()
,应用程序将无响应。你应该改为使用singleShot方法进行调查。你能补充一些你遇到的问题的细节吗?问题没有提到什么不适合您。我的问题是我不会打开window2或window3。我认为这是一个无止境的循环