from PyQt5.QtCore import Qt, QObject, QBasicTimer from PyQt5.QtWidgets import QApplication, QLabel class MyTimer(QObject): def __init__(self, label): super().__init__() self.timer = QBasicTimer() self.label = label self.count = 0 def start(self): self.timer.start(1000, self) def stop(self): self.timer.stop() def timerEvent(self, event): self.count += 1 self.label.setText(f"Time elapsed: {self.count} seconds") app = QApplication([]) label = QLabel("Time elapsed: 0 seconds") timer = MyTimer(label) timer.start() label.show() app.exec_()
from PyQt5.QtCore import Qt, QObject, QBasicTimer from PyQt5.QtWidgets import QApplication, QMainWindow, QProgressBar class MyTimer(QObject): def __init__(self, progress_bar): super().__init__() self.timer = QBasicTimer() self.progress_bar = progress_bar self.count = 0 def start(self): self.timer.start(100, self) def stop(self): self.timer.stop() def timerEvent(self, event): self.count += 1 value = min(self.count, 100) self.progress_bar.setValue(value) app = QApplication([]) window = QMainWindow() progress_bar = QProgressBar() timer = MyTimer(progress_bar) timer.start() window.setCentralWidget(progress_bar) window.show() app.exec_()This code creates a timer that updates a progress bar every 100 ms (0.1 seconds). The `MyTimer` class inherits `QObject` and handles the timer events using the `timerEvent` method. The progress bar value is set to the current count and capped at 100. In conclusion, PyQt5 is the package library used in the above examples, which uses QBasicTimer for handling timer events. These examples show how to create simple timers that update labels or progress bars.