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

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

我正在 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()

简而言之:我如何使用 time.sleep(),每一秒都做一些不同的事情。有什么建议吗?

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"

这是否符合您的要求?

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)

你可以

import time

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

或者在你的例子中...

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()

您可以使用条件语句只循环直到 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()