pyqt5 qtimer
时间: 2023-06-22 11:37:55 浏览: 107
PyQt5中的QTimer类是一个用于发射信号的定时器。您可以使用它来执行周期性操作,例如更新UI或执行后台任务。下面是一个简单的示例,演示如何在每个一定时间间隔内更新一个标签的文本:
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout
class MyWidget(QWidget):
def __init__(self):
super().__init__()
layout = QVBoxLayout()
self.label = QLabel("Hello world!")
layout.addWidget(self.label)
self.setLayout(layout)
self.timer = QTimer()
self.timer.timeout.connect(self.update_label)
self.timer.start(1000) # 1秒钟更新一次
def update_label(self):
self.label.setText("Updated at " + QDateTime.currentDateTime().toString())
if __name__ == '__main__':
app = QApplication([])
widget = MyWidget()
widget.show()
app.exec_()
在上面的示例中,我们创建了一个名为MyWidget的小部件,并在其中添加了一个标签。我们还创建了一个QTimer对象,并将其timeout信号连接到一个名为update_label的方法。在update_label方法中,我们更新了标签的文本。最后,我们启动了定时器,使其每秒钟发射一次timeout信号。
相关推荐
















