赞
踩
startTimer(ms, Qt.TimerType) 开启一个定时器。返回值为timer_id。
TimerType:(1)Qt.PreciseTimer,精确定时器:尽可能保持毫秒准确。
(2)Qt.CoarseTimer,粗定时器:5%的误差间隔。
(3)Qt.VeryCoarseTimer,很粗的定时器:只能到秒级。
killTimer(timer_id) 根据定时器ID,杀死定时器。
timerEvent() 定时器执行事件。
class mylabel(QLabel):是对QLabel的继承,方便对def timerEvent(self, *args,**kwargs):的重新。
- 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
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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。