Skip to content Skip to sidebar Skip to footer

PyQt5 QTimer Count Until Specific Seconds

I am creating a program in python and i am using pyqt. I am currently working with the QTimer and i want to print 'timer works' every seconds and stop printing after 5 seconds. Her

Solution 1:

Below is a simple demo showing how to create a timer that stops after a fixed number of timeouts.

from PyQt5 import QtCore

def start_timer(slot, count=1, interval=1000):
    counter = 0
    def handler():
        nonlocal counter
        counter += 1
        slot(counter)
        if counter >= count:
            timer.stop()
            timer.deleteLater()
    timer = QtCore.QTimer()
    timer.timeout.connect(handler)
    timer.start(interval)

def timer_func(count):
    print('Timer:', count)
    if count >= 5:
        QtCore.QCoreApplication.quit()

app = QtCore.QCoreApplication([])
start_timer(timer_func, 5)
app.exec_()

Post a Comment for "PyQt5 QTimer Count Until Specific Seconds"