PyQt5中QTimer定時(shí)器的實(shí)例代碼
如果要在應(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)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信