SoFunction
Updated on 2024-10-29

Example code for QTimer timer in PyQt5

If you want to perform an operation periodically in your application, such as periodically checking the CPU value of the host computer, you need to use a QTimer timer. The QTimer class provides both repetitive and one-shot timers. To use a timer, you need to create a QTimer instance, connect its timeout signal to the appropriate slot, and call thestart(). The timer then sends out timeout signals at constant intervals, and when the window control receives the timeout signal, it stops this timer.

I. Common methods in the QTimer class

methodologies descriptive
start(milliseconds) Starts or restarts the timer in millisecond intervals. If the timer is already running, it will be stopped and restarted. If the singleShot signal is true, the timer will be activated only once
Stop() Stop Timer

Second, the QTimer class in the common signals

code descriptive
singleShot Fires this signal when a slot function is called after a given time interval
timeout Emits this signal when the timer times out

III. Use of QTimer

Example 1:

import sys
from PyQt5 import QtCore
from  import *
from  import *
from  import *

class Demo(QWidget):
    count = 0
    def __init__(self):
        super().__init__()
        (100, 50, 500, 400)
        ('QTimer')

         = QListWidget()
         = QLabel('Show current time')
         = QPushButton('Start')
         = QPushButton('The End')
        layout = QGridLayout()

        # Initialize the timer
         = QTimer(self)
        ()
        ()
        ()

        (,0,0,1,2)
        (,1,0)
        (,1,1)
        (layout)

    def showTime(self):
        # Get the system's current time
        time = ().toString('yyyy-MM-dd hh:mm:ss dddd')
        (time)

    def startTimer(self):
        # Set the interval and start the timer
        (1000)
        (False)
        (True)

    def endTimer(self):
        #Shut down the timer
        ()
        (True)
        (False)

if __name__ == "__main__":
    app = QApplication()
    form = Demo()
    ()
    (app.exec_())

The running effect is as follows:

在这里插入图片描述

Example 2:

import sys
from PyQt5 import QtCore
from  import *
from  import *
from  import *

if __name__ == "__main__":
    app = QApplication()
    label = QLabel('<font color=blue size=20><b>PyQt5,computer operating system window5disappear after a second</b></font>')
    #Borderless windows
    (|)
    ()
    #Set auto exit after 5 seconds
    (5000,)
    (app.exec_())

The running effect is as follows:

在这里插入图片描述

PyQt5 QTimer counts to a specific number of seconds

I am creating a program in python and I am using pyqt. I am currently using QTimer and I want to print "timer works" every second and stop printing after 5 seconds. Here is my code:

timers = []
def thread_func():
    print("Thread works")
    timer = ()
    (timer_func)
    (1000)
    print(())
    print(())
    (timer)

def timer_func():
    print("Timer works")

prescription

Here 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 = ()
    (handler)
    (interval)

def timer_func(count):
    print('Timer:', count)
    if count >= 5:
        ()

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

This article on the PyQt5 QTimer timer example code is introduced to this article, more related PyQt5 QTimer timer content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!