佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > PyQt5中QTimer定時(shí)器的實(shí)例代碼

PyQt5中QTimer定時(shí)器的實(shí)例代碼

熱門標(biāo)簽:網(wǎng)站文章發(fā)布 銀行業(yè)務(wù) 鐵路電話系統(tǒng) 檢查注冊(cè)表項(xiàng) 呼叫中心市場(chǎng)需求 服務(wù)器配置 智能手機(jī) 美圖手機(jī)

如果要在應(yīng)用程序中周期性地進(jìn)行某項(xiàng)操作,比如周期性地檢測(cè)主機(jī)的CPU值,則需要用到QTimer定時(shí)器,QTimer類提供了重復(fù)的和單次的定時(shí)器。要使用定時(shí)器,需要先創(chuàng)建一個(gè)QTimer實(shí)例,將其timeout信號(hào)連接到相應(yīng)的槽,并調(diào)用start()。然后定時(shí)器會(huì)以恒定的間隔發(fā)出timeout信號(hào),當(dāng)窗口控件收到timeout信號(hào)后,它就會(huì)停止這個(gè)定時(shí)器。

一、QTimer類中的常用方法

方法 描述
start(milliseconds) 啟動(dòng)或重新啟動(dòng)定時(shí)器,時(shí)間間隔為毫秒。如果定時(shí)器已經(jīng)運(yùn)行,它將被停止并重新啟動(dòng)。如果singleShot信號(hào)為真,定時(shí)器將僅被激活一次
Stop() 停止定時(shí)器

二、QTimer類中的常用信號(hào)

信號(hào) 描述
singleShot 在給定的時(shí)間間隔后調(diào)用一個(gè)槽函數(shù)時(shí)發(fā)射此信號(hào)
timeout 當(dāng)定時(shí)器超時(shí)時(shí)發(fā)射此信號(hào)

三、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('顯示當(dāng)前時(shí)間')
        self.start = QPushButton('開始')
        self.end = QPushButton('結(jié)束')
        layout = QGridLayout()

        #初始化定時(shí)器
        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):
        #獲取系統(tǒng)現(xiàn)在的時(shí)間
        time = QDateTime.currentDateTime().toString('yyyy-MM-dd hh:mm:ss dddd')
        self.label.setText(time)

    def startTimer(self):
        #設(shè)置時(shí)間間隔并啟動(dòng)定時(shí)器
        self.timer.start(1000)
        self.start.setEnabled(False)
        self.end.setEnabled(True)

    def endTimer(self):
        #關(guān)閉定時(shí)器
        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_())

運(yùn)行效果如下:


示例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()
    #設(shè)置5秒后自動(dòng)退出
    QTimer.singleShot(5000,app.quit)
    sys.exit(app.exec_())

運(yùn)行效果如下:

PyQt5 QTimer計(jì)數(shù)到特定的秒數(shù)

我正在使用python創(chuàng)建程序,并且正在使用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")

解決方案

以下是一個(gè)簡(jiǎn)單的演示,顯示了如何創(chuàng)建在固定數(shù)量的超時(shí)后停止計(jì)時(shí)的計(jì)時(shí)器。

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

到此這篇關(guān)于PyQt5中QTimer定時(shí)器的實(shí)例代碼的文章就介紹到這了,更多相關(guān)PyQt5 QTimer定時(shí)器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • python GUI庫圖形界面開發(fā)之PyQt5時(shí)間控件QTimer詳細(xì)使用方法與實(shí)例
  • PyQt5使用QTimer實(shí)現(xiàn)電子時(shí)鐘

標(biāo)簽:樂山 上海 新疆 長(zhǎng)治 河南 滄州 紅河 沈陽

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PyQt5中QTimer定時(shí)器的實(shí)例代碼》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    钟山县| 茂名市| 闸北区| 山阴县| 荥经县| 定陶县| 榆中县| 金山区| 突泉县| 水富县| 电白县| 城口县| 安图县| 云林县| 资中县| 阜南县| 犍为县| 宁晋县| 诸城市| 都江堰市| 荃湾区| 伊金霍洛旗| 太谷县| 新疆| 晋中市| 文登市| 来宾市| 丽江市| 无棣县| 越西县| 新邵县| 青铜峡市| 昂仁县| 福贡县| 宁化县| 九龙城区| 云浮市| 宁远县| 玛多县| 颍上县| 汨罗市|