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)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pycharm打開長(zhǎng)代碼文件CPU占用率過高的解決
這篇文章主要介紹了pycharm打開長(zhǎng)代碼文件CPU占用率過高的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09詳解Python+Selenium+ChromeDriver的配置和問題解決
這篇文章主要介紹了Python+Selenium+ChromeDriver的配置和問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Python?selenium?find_element()示例詳解
selenium定位元素的函數(shù)/方法可以分為兩類:find_element及find_elements,下面這篇文章主要給大家介紹了關(guān)于Python?selenium?find_element()的相關(guān)資料,需要的朋友可以參考下2022-07-07Python 使用 PyQt5 開發(fā)的關(guān)機(jī)小工具分享
這篇文章主要介紹了Python 使用 PyQt5 開發(fā)的關(guān)機(jī)小工具分享,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07Python使用googletrans報(bào)錯(cuò)的解決方法
這篇文章主要給大家介紹了關(guān)于Python使用googletrans報(bào)錯(cuò)的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09Python中的相關(guān)分析correlation analysis的實(shí)現(xiàn)
這篇文章主要介紹了Python中的相關(guān)分析correlation analysis的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08