Hardware and Software Environment
- OS X EI Capitan
- Python 3.5.1
- PyQt 5.5.1
Objective
If you need to do something periodically in a program, such as detecting the state of a device, a timer is used. This article looks at the use of Qtimer in PyQT5.
Qtimer Example
Suppose you want to implement this function every second, and see how Qtimer is implemented?
self.timer = QTimer(self)self.count = 0self.timer.timeout.connect(self.showNum)self.startCount()
First instantiate a timer and then bind its timeout signal to the custom Shownum () method. Finally call Startcount () This method to start the timer. The above statements are usually placed in the constructor of window or form. The complete example is to write it yourself, and it's very simple.
Startcount () method
def startcount (self):
Self.timer.start (1000)
The Timer.start () time parameter is in milliseconds, note that the Timer timeout () is triggered once per second, and the signal timeout is bound to the slot shownum (). So Shownum () is also called once.
Shownum () slot function
def shownum (self):
Count = Count + 1
Print (count)
PYQT5 Series Tutorials (eight) use of timer Qtimer