QTimer
QTimer 是 PyQt6 中的一个类,用于创建定时器。它允许你在指定的时间间隔内重复执行某个操作,或者在一段时间后执行一次操作。QTimer 通常用于需要定时更新界面、执行后台任务或处理周期性事件的场景。
主要功能
-
单次定时:在指定的时间间隔后触发一次操作。
-
重复定时:以固定的时间间隔重复触发操作。
-
精确控制:可以启动、停止、重启定时器,并动态调整时间间隔。
-
信号机制:通过信号与槽机制,可以在定时器触发时执行特定的函数。
常用方法
-
start(int):启动定时器,参数为时间间隔(毫秒)。 -
stop():停止定时器。 -
setInterval(int):设置定时器的时间间隔(毫秒)。 -
interval():获取当前定时器的时间间隔。 -
isActive():判断定时器是否正在运行。 -
singleShot(int, callable):静态方法,用于在指定时间后执行一次操作。
信号
-
timeout():当定时器超时时发出的信号。可以将其连接到槽函数,以执行定时任务。
示例代码
使用Qt Designer 设计的界面:
test_ui.py
# Form implementation generated from reading ui file 'test_ui.ui'
#
# Created by: PyQt6 UI code generator 6.8.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt6 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(640, 480)
self.lcd = QtWidgets.QLCDNumber(parent=Form)
self.lcd.setGeometry(QtCore.QRect(255, 30, 88, 25))
self.lcd.setLayoutDirection(QtCore.Qt.LayoutDirection.LeftToRight)
self.lcd.setObjectName("lcd")
self.pushButton = QtWidgets.QPushButton(parent=Form)
self.pushButton.setGeometry(QtCore.QRect(250, 52, 51, 32))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(parent=Form)
self.pushButton_2.setGeometry(QtCore.QRect(300, 52, 51, 32))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(parent=Form)
self.pushButton_3.setGeometry(QtCore.QRect(255, 75, 81, 32))
self.pushButton_3.setObjectName("pushButton_3")
self.textBrowser = QtWidgets.QTextBrowser(parent=Form)
self.textBrowser.setGeometry(QtCore.QRect(180, 102, 256, 192))
self.textBrowser.setObjectName("textBrowser")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "开始"))
self.pushButton_2.setText(_translate("Form", "停止"))
self.pushButton_3.setText(_translate("Form", "查询状态"))
main.py
import sys
from PyQt6.QtCore import QTimer
from PyQt6.QtWidgets import *
from test_ui import *
class Window(QWidget,Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
self.count = 0
self.timer = QTimer()
self.timer.setInterval(1000) # 触发周期1s
self.timer.timeout.connect(self.update_cnt)
# 消息 槽绑定
self.pushButton.clicked.connect(self.timer_start)
self.pushButton_2.clicked.connect(self.timer_stop)
self.pushButton_3.clicked.connect(self.timer_state)
def update_cnt(self):
self.count = self.count + 1
self.lcd.display(self.count)
# 设置启动
def timer_start(self):
self.timer.start()
self.textBrowser.clear()
# 设置停止
def timer_stop(self):
self.timer.stop()
self.textBrowser.clear()
# 查看状态
def timer_state(self):
state = self.timer.isActive()
if state:
self.textBrowser.setText(f'定时器启动中...')
else:
self.textBrowser.setText(f'定时器已停止!')
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
应用场景
-
界面更新:定时刷新界面内容,例如时钟、进度条等。
-
后台任务:定期执行后台任务,例如数据采集、文件检查等。
-
动画效果:实现简单的动画效果,例如轮播图、动态图标等。
-
延迟操作:在指定时间后执行某个操作,例如延迟关闭窗口、延迟提示等。
总结
QTimer 是 PyQt6 中用于定时任务的核心工具,具有以下特点:
-
支持单次定时和重复定时。
-
通过信号与槽机制实现定时任务的触发。
-
提供精确的控制方法,例如启动、停止、调整间隔等。
-
适用于界面更新、后台任务、动画效果等多种场景。
2160

被折叠的 条评论
为什么被折叠?



