gpt4 book ai didi

PyQt5中QTimer定时器的实例代码

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 27 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章PyQt5中QTimer定时器的实例代码由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

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

1、QTimer类中的常用方法

  。

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

  。

2、QTimer类中的常用信号

  。

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

  。

3、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 。

最后此篇关于PyQt5中QTimer定时器的实例代码的文章就讲到这里了,如果你想了解更多关于PyQt5中QTimer定时器的实例代码的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com