Learn Python PyQt

QTimer example for PyQt5

If you’re developing applications where specific tasks are executed periodically, the QTimer from PyQt5 is an indispensable tool. For instance, consider an application that routinely checks the host’s CPU value.

QTimer ensures functionality by emitting a Timeout signal. On receiving this signal, the window’s control will cease the timer.
Two primary methods of QTimer include start(milliseconds) for initiating the timer and Stop() to halt it.

Recommended Reading: Create Desktop Apps with Python PyQt5

Working with QTimer

In the sample application described below, we have two buttons: start and stop. Initiating the start button activates the QTimer, updating the time display every second.

pyqt timer qtimer

import sys
from PyQt5.QtWidgets import QWidget,QPushButton,QApplication,QListWidget,QGridLayout,QLabel
from PyQt5.QtCore import QTimer,QDateTime

class WinForm(QWidget):
    def __init__(self,parent=None):
        super(WinForm, self).__init__(parent)
        self.setWindowTitle('QTimer demonstration')

        self.listFile=QListWidget()
        self.label=QLabel('Label')
        self.startBtn=QPushButton('Start')
        self.endBtn=QPushButton('Stop')

        layout=QGridLayout()

        self.timer=QTimer()
        self.timer.timeout.connect(self.showTime)

        layout.addWidget(self.label,0,0,1,2)
        layout.addWidget(self.startBtn,1,0)
        layout.addWidget(self.endBtn,1,1)

        self.startBtn.clicked.connect(self.startTimer)
        self.endBtn.clicked.connect(self.endTimer)

        self.setLayout(layout)

    def showTime(self):
        current_time=QDateTime.currentDateTime()
        formatted_time=current_time.toString('yyyy-MM-dd hh:mm:ss dddd')
        self.label.setText(formatted_time)

    def startTimer(self):
        self.timer.start(1000)
        self.startBtn.setEnabled(False)
        self.endBtn.setEnabled(True)

    def endTimer(self):
        self.timer.stop()
        self.startBtn.setEnabled(True)
        self.endBtn.setEnabled(False)

if __name__ == '__main__':
    app=QApplication(sys.argv)
    form=WinForm()
    form.show()
    sys.exit(app.exec_())

Let’s break down the critical functions:

  1. A timer is initialized and its timeout signal is connected to the showTime() function:

     self.timer=QTimer(self)
     self.timer.timeout.connect(self.showTime)
    
  2. The showTime() function retrieves and displays the system’s current time:

    def showTime(self):
    current_time=QDateTime.currentDateTime()
    formatted_time=current_time.toString(‘yyyy-MM-dd hh:mm:ss dddd’)
    self.label.setText(formatted_time)

  3. On pressing the Start button, the timer starts and the button is deactivated:

     self.timer.start(1000)
     self.startBtn.setEnabled(False)
     self.endBtn.setEnabled(True)
    
  4. The end button halts the timer and disables itself:

     self.timer.stop()
     self.startBtn.setEnabled(True)
     self.endBtn.setEnabled(False)
    

Deep Dive: Create Desktop Apps with Python PyQt5