当前位置:   article > 正文

PyQt5-QObject(06)-定时器_pyqt5 定时器

pyqt5 定时器

定时器

API

startTimer(ms, Qt.TimerType)         开启一个定时器。返回值为timer_id。

TimerType:(1)Qt.PreciseTimer,精确定时器:尽可能保持毫秒准确。

                     (2)Qt.CoarseTimer,粗定时器:5%的误差间隔。

                     (3)Qt.VeryCoarseTimer,很粗的定时器:只能到秒级。

killTimer(timer_id) 根据定时器ID,杀死定时器。

timerEvent() 定时器执行事件。

API Dome

class mylabel(QLabel):是对QLabel的继承,方便对def timerEvent(self, *args,**kwargs):的重新。

  1. from PyQt5.Qt import *
  2. import sys
  3. class Window(QWidget):
  4. def __init__(self):
  5. super().__init__()
  6. self.setWindowTitle("QObject learning")
  7. self.resize(500,500)
  8. self.setup_ui()
  9. def setup_ui(self):
  10. self.Timer()
  11. def Timer(self):
  12. class mylabel(QLabel):
  13. def timerEvent(self, *args,**kwargs):
  14. num=int(lab.text())
  15. num =num-1
  16. print(num)
  17. lab.setText(str(num))
  18. if num==0:
  19. print("停止")
  20. lab.killTimer(timer_id)
  21. lab=mylabel(self)
  22. lab.setText('10')
  23. timer_id=lab.startTimer(1000)
  24. if __name__ == '__main__':
  25. app=QApplication(sys.argv)
  26. window=Window()
  27. window.show()
  28. sys.exit(app.exec())
  29. Running result:
  30. D:\mypython\Pyqt5learning\Scripts\python.exe D:/mypython/Pyqt5learning/Dome8.py
  31. 9
  32. 8
  33. 7
  34. 6
  35. 5
  36. 4
  37. 3
  38. 2
  39. 1
  40. 0
  41. Process finished with exit code 0
from PyQt5.Qt import *
import sys
​
class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QObject learning")
        self.resize(500,500)
        self.setup_ui()
​
    def setup_ui(self):
        self.Timer()
​
    def Timer(self):
        class mylabel(QLabel):
            def timerEvent(self, *args,**kwargs):
                num=int(lab.text())
                num  =num-1
                print(num)
                lab.setText(str(num))
                if num==0:
                    print("停止")
                    lab.killTimer(timer_id)
        lab=mylabel(self)
        lab.setText('10')
        timer_id=lab.startTimer(1000)
​
if __name__ == '__main__':
     app=QApplication(sys.argv)
     window=Window()
     window.show()
     sys.exit(app.exec())
    
    
Running result: 
D:\mypython\Pyqt5learning\Scripts\python.exe D:/mypython/Pyqt5learning/Dome8.py 
9
8
7
6
5
4
3
2
1
0
​
Process finished with exit code 0
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/854065
推荐阅读
相关标签
  

闽ICP备14008679号