Lv.的博客

PyQt5 定时器事件

from PyQt5.Qt import *
import sys


#
# class Obj(QObject):
#     def timerEvent(self, QTimerEvent):
#         print(QTimerEvent, 2)


class Label(QLabel):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.resize(80, 40)
        self.move(100, 100)
        self.setText('10')
        self.setStyleSheet("background-color:#02CDB9;font-size:25px")

    def timerEvent(self, *args, **kwargs):
        print('事件案例演示')
        # 获取text中的数据
        sec = int(self.text())
        # 倒计时
        sec -= 1
        self.setText(str(sec))
        # 停止计时
        # if sec == 0:
        #     self.killTimer(timer_id)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = QWidget()
    window.resize(600, 500)

    label = Label(window)

    timer_id = label.startTimer(1000)

    # label.killTimer(timer_id)

    window.show()
    sys.exit(app.exec_())

 

posted @ 2022-05-31 16:19  Avatarx  阅读(83)  评论(0编辑  收藏  举报