Storm Shadow
Storm Shadow

Reputation: 462

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.

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. 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"

Upvotes: 0

Views: 258

Answers (2)

p-robot
p-robot

Reputation: 4904

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)

Upvotes: 0

dot.Py
dot.Py

Reputation: 5157

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

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

Upvotes: 1

Related Questions