基本使用
timer = QTimer(self)
timer.timeout.connect(func)
timer.start(1000)
定时器
import sys
from PyQt5.QtWidgets import *
class MyWidget(QWidget):
def timerEvent(self, *args, **kwargs):
cur_w = self.width()
cur_h = self.height()
self.resize(cur_w + 1, cur_h + 1)
if cur_w == 600 or cur_h == 360:
self.killTimer(self.timer_id)
def set_timer(self, ms):
self.timer_id = self.startTimer(ms)
class MyLabel(QLabel):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setText('10')
self.move(200, 100)
self.setStyleSheet('font-size:22px;')
def timerEvent(self, *args, **kwargs):
cur_text = int(self.text())
if cur_text > 0:
cur_text -= 1
self.setText(str(cur_text))
if cur_text == 0:
self.killTimer(self.timer_id)
def set_timer(self, ms):
self.timer_id = self.startTimer(ms)
def set_seconds(self, seconds):
self.setText(str(seconds))
if __name__ == '__main__':
app = QApplication(sys.argv)
root = MyWidget()
root.setWindowTitle('')
root.resize(500, 300)
root.move(400, 400)
root.set_timer(110)
label = MyLabel(root)
label.set_timer(500)
label.set_seconds(20)
root.show()
sys.exit(app.exec_())
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9e3217214ba2631cfccb6f8d56810b74.png#pic_center)