简体   繁体   English

我如何使用time.sleep,每秒做不同的事情

[英]How do i use time.sleep , and do something different each second

I am making a PyQt5 splash screen in PyQt that creates a countdown within a splash picture, but the script isn't behaving right and it has different result each time. 我在PyQt中制作了一个PyQt5启动画面,该画面在启动画面中创建了倒数计时,但是脚本运行不正常,并且每次都有不同的结果。

from PyQt5 import QtGui, QtCore, QtWidgets
from PyQt5.QtGui import QFont, QPixmap
from PyQt5.QtWidgets import QSplashScreen
import os
import sys
import time
from time import sleep



count = [1,2,3,4,5]

def Splash(self):
    app.processEvents()
    splash = QtWidgets.QSplashScreen(QtGui.QPixmap("22.png"))
    font = splash.font()
    font.setPixelSize(16)
    font.setWeight(QFont.Bold)
    splash.setFont(font)
    #splash.showMessage(str(count[0]), QtCore.Qt.AlignHCenter | QtCore.Qt.AlignBottom, QtCore.Qt.white )
    #splash.show()
    for i in range(0, 10):
        time.sleep(1)
        splash = QtWidgets.QSplashScreen(QtGui.QPixmap("22.png"))
        splash.showMessage(str(count[0]), QtCore.Qt.AlignHCenter | QtCore.Qt.AlignBottom, QtCore.Qt.white)
        QtWidgets.QApplication.processEvents()
        splash.show()
        time.sleep(1)
        splash = QtWidgets.QSplashScreen(QtGui.QPixmap("22.png"))
        splash.showMessage(str(count[1]), QtCore.Qt.AlignHCenter | QtCore.Qt.AlignBottom, QtCore.Qt.white)
        QtWidgets.QApplication.processEvents()
        splash.show()
        time.sleep(1)
        splash = QtWidgets.QSplashScreen(QtGui.QPixmap("22.png"))
        splash.showMessage(str(count[2]), QtCore.Qt.AlignHCenter | QtCore.Qt.AlignBottom, QtCore.Qt.white)
        QtWidgets.QApplication.processEvents()
        splash.show()
        time.sleep(1)
        splash = QtWidgets.QSplashScreen(QtGui.QPixmap("22.png"))
        splash.showMessage(str(count[3]), QtCore.Qt.AlignHCenter | QtCore.Qt.AlignBottom, QtCore.Qt.white)
        QtWidgets.QApplication.processEvents()
        splash.show()
        time.sleep(1)
        splash = QtWidgets.QSplashScreen(QtGui.QPixmap("22.png"))
        splash.showMessage(str(count[4]), QtCore.Qt.AlignHCenter | QtCore.Qt.AlignBottom, QtCore.Qt.white)
        QtWidgets.QApplication.processEvents()
        splash.show()

In short terms: How do I use time.sleep(), and every second do something different. 简而言之:我如何使用time.sleep(),并且每一秒都做不同的事情。 Any suggestions? 有什么建议么?

import time


for i in range(0, 10):
    time.sleep(1)
    print "1"
    time.sleep(1)
    print "2"
    time.sleep(1)
    print "3"
    time.sleep(1)
    print "4"
    time.sleep(1)
    print "5"

You can 您可以

import time

for i in range(0, 10):
    time.sleep(0)
    print i

Or in your example... 或者在您的示例中...

for i in range(0, 10):
    time.sleep(1)
    splash = QtWidgets.QSplashScreen(QtGui.QPixmap("22.png"))
    splash.showMessage(str(count[i]), QtCore.Qt.AlignHCenter | QtCore.Qt.AlignBottom, QtCore.Qt.white)
    QtWidgets.QApplication.processEvents()
    splash.show()

You can either use a conditional statement to loop only until i <= 4 您可以使用条件语句仅循环直到i <= 4

for i in range(0, 10):
    time.sleep(1)
    splash = QtWidgets.QSplashScreen(QtGui.QPixmap("22.png"))
    if i <= 4:
        splash.showMessage(str(count[i]), QtCore.Qt.AlignHCenter | QtCore.Qt.AlignBottom, QtCore.Qt.white)
    else:
        QtWidgets.QApplication.processEvents()
    splash.show()

Does this do what you want it to do? 这是您想要的吗?

import time
for i in range(10):
    time.sleep(1)
    print(i)
    time.sleep(1)
    print("New command; Iteration:", i)
    time.sleep(1)
    print("New new command; Iteration:", i)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM