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

时间:2016-07-19 19:27:25

标签: python time process pyqt5

我在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"

    

2 个答案:

答案 0 :(得分:1)

你可以

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

答案 1 :(得分:0)

这是否符合您的要求?

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)