脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - PyQt5中QTimer定时器的实例代码

PyQt5中QTimer定时器的实例代码

2021-11-19 13:46Anony吧 Python

如果需要在程序中周期性地进行某项操作,比如检测某种设备的状态,就会用到定时器,本文主要介绍了PyQt5中QTimer定时器的实例代码,感兴趣的可以了解一下

如果要在应用程序中周期性地进行某项操作,比如周期性地检测主机的CPU值,则需要用到QTimer定时器,QTimer类提供了重复的和单次的定时器。要使用定时器,需要先创建一个QTimer实例,将其timeout信号连接到相应的槽,并调用start()。然后定时器会以恒定的间隔发出timeout信号,当窗口控件收到timeout信号后,它就会停止这个定时器。

一、QTimer类中的常用方法

 

方法 描述
start(milliseconds) 启动或重新启动定时器,时间间隔为毫秒。如果定时器已经运行,它将被停止并重新启动。如果singleShot信号为真,定时器将仅被激活一次
Stop() 停止定时器

 

二、QTimer类中的常用信号

 

信号 描述
singleShot 在给定的时间间隔后调用一个槽函数时发射此信号
timeout 当定时器超时时发射此信号

 

三、QTimer的使用

示例1:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import sys
from PyQt5 import QtCore
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
 
class Demo(QWidget):
    count = 0
    def __init__(self):
        super().__init__()
        self.setGeometry(100, 50, 500, 400)
        self.setWindowTitle('QTimer')
 
        self.list = QListWidget()
        self.label = QLabel('显示当前时间')
        self.start = QPushButton('开始')
        self.end = QPushButton('结束')
        layout = QGridLayout()
 
        #初始化定时器
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.showTime)
        self.start.clicked.connect(self.startTimer)
        self.end.clicked.connect(self.endTimer)
 
        layout.addWidget(self.label,0,0,1,2)
        layout.addWidget(self.start,1,0)
        layout.addWidget(self.end,1,1)
        self.setLayout(layout)
 
    def showTime(self):
        #获取系统现在的时间
        time = QDateTime.currentDateTime().toString('yyyy-MM-dd hh:mm:ss dddd')
        self.label.setText(time)
 
    def startTimer(self):
        #设置时间间隔并启动定时器
        self.timer.start(1000)
        self.start.setEnabled(False)
        self.end.setEnabled(True)
 
    def endTimer(self):
        #关闭定时器
        self.timer.stop()
        self.start.setEnabled(True)
        self.end.setEnabled(False)
 
if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = Demo()
    form.show()
    sys.exit(app.exec_())

运行效果如下:

PyQt5中QTimer定时器的实例代码

示例2:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import sys
from PyQt5 import QtCore
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
 
if __name__ == "__main__":
    app = QApplication(sys.argv)
    label = QLabel('<font color=blue size=20><b>PyQt5,窗口5秒后消失</b></font>')
    #无边框窗口
    label.setWindowFlags(Qt.SplashScreen|Qt.FramelessWindowHint)
    label.show()
    #设置5秒后自动退出
    QTimer.singleShot(5000,app.quit)
    sys.exit(app.exec_())

运行效果如下:

PyQt5中QTimer定时器的实例代码

PyQt5 QTimer计数到特定的秒数

我正在使用python创建程序,并且正在使用pyqt。我目前正在使用QTimer,我想每秒钟打印一次“ timer works”,并在5秒钟后停止打印。这是我的代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
timers = []
def thread_func():
    print("Thread works")
    timer = QtCore.QTimer()
    timer.timeout.connect(timer_func)
    timer.start(1000)
    print(timer.remainingTime())
    print(timer.isActive())
    timers.append(timer)
 
def timer_func():
    print("Timer works")

解决方案

以下是一个简单的演示,显示了如何创建在固定数量的超时后停止计时的计时器。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from PyQt5 import QtCore
 
def start_timer(slot, count=1, interval=1000):
    counter = 0
    def handler():
        nonlocal counter
        counter += 1
        slot(counter)
        if counter >= count:
            timer.stop()
            timer.deleteLater()
    timer = QtCore.QTimer()
    timer.timeout.connect(handler)
    timer.start(interval)
 
def timer_func(count):
    print('Timer:', count)
    if count >= 5:
        QtCore.QCoreApplication.quit()
 
app = QtCore.QCoreApplication([])
start_timer(timer_func, 5)
app.exec_()

到此这篇关于PyQt5中QTimer定时器的实例代码的文章就介绍到这了,更多相关PyQt5 QTimer定时器内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_44880255/article/details/107702123

延伸 · 阅读

精彩推荐