婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

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

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

熱門(mén)標(biāo)簽:小蘇云呼電話機(jī)器人 市場(chǎng)上的電銷(xiāo)機(jī)器人 地圖標(biāo)注面積 佛山400電話辦理 所得系統(tǒng)電梯怎樣主板設(shè)置外呼 北京電銷(xiāo)外呼系統(tǒng)加盟 儋州電話機(jī)器人 朝陽(yáng)手機(jī)外呼系統(tǒng) 北瀚ai電銷(xiāo)機(jī)器人官網(wǎng)手機(jī)版

如果要在應(yīng)用程序中周期性地進(jìn)行某項(xiàng)操作,比如周期性地檢測(cè)主機(jī)的CPU值,則需要用到QTimer定時(shí)器,QTimer類(lèi)提供了重復(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類(lèi)中的常用方法

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

二、QTimer類(lèi)中的常用信號(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('開(kāi)始')
        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>')
    #無(wú)邊框窗口
    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庫(kù)圖形界面開(kāi)發(fā)之PyQt5時(shí)間控件QTimer詳細(xì)使用方法與實(shí)例
  • PyQt5使用QTimer實(shí)現(xiàn)電子時(shí)鐘

標(biāo)簽:江蘇 寧夏 龍巖 云南 酒泉 定西 金融催收 商丘

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PyQt5中QTimer定時(shí)器的實(shí)例代碼》,本文關(guān)鍵詞  PyQt5,中,QTimer,定時(shí)器,的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《PyQt5中QTimer定時(shí)器的實(shí)例代碼》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于PyQt5中QTimer定時(shí)器的實(shí)例代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 会东县| 尉犁县| 尉氏县| 千阳县| 专栏| 元朗区| 全州县| 新巴尔虎右旗| 玉山县| 花莲市| 田林县| 小金县| 勃利县| 神木县| 巴中市| 龙口市| 仪征市| 龙井市| 和硕县| 凤翔县| 绥化市| 南乐县| 北京市| 栾川县| 赤城县| 柏乡县| 玉环县| 哈巴河县| 贵州省| 黎平县| 肃南| 临沧市| 庄浪县| 措勤县| 两当县| 景洪市| 哈尔滨市| 莫力| 南城县| 伊通| 江永县|