PyQt5定时器QTimer的简单操作

2022-10-21 12:26:14

当代程序中需要显示时间时或者需要在程序中周期性地进行某项操作,就会用到定时器。PyQt5就提供了一个定时器QTimer来实现这种操作
1.导入QTimer模块

from PyQt5.QtCoreimport QTimer

2.初始化

#在类中定义一个定时器,并在构造函数中设置启动及其信号和槽
self.timer = QTimer(self)#设置计时间隔并启动(1000ms == 1s)
self.timer.start(1000)#计时结束调用timeout_slot()方法,注意不要加()
self.timer.timeout.connect(self.timeout_slot)

3.槽函数

deftimeout(self):# 获取当前系统时间
   time = QDateTime.currentDateTime()# 时间显示格式
   str = time.toString("yyyy-mm-dd hh:mm:ss");
   self.lcdNumber.display(str);
  • 作者:这、一年
  • 原文链接:https://blog.csdn.net/qq_29666899/article/details/79118404
    更新时间:2022-10-21 12:26:14