当前位置 博文首页 > PyQt5中QTimer定时器的实例代码

    PyQt5中QTimer定时器的实例代码

    作者:Anony吧 时间:2021-08-11 18:48

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

    一、QTimer类中的常用方法

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

    二、QTimer类中的常用信号

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

    三、QTimer的使用

    示例1:

    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_())
    

    运行效果如下:

    在这里插入图片描述

    示例2:

    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计数到特定的秒数

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

    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")
    

    解决方案

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

    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定时器内容请搜索站长博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持站长博客!

    jsjbwy