Python PyQt5 QTimer 定時器用法與範例

本篇 ShengYu 介紹 Python PyQt5 QTimer 定時器用法與範例,QTimer 定時器可以作間隔一段時間就要執行任務的相關應用,例如時鐘、更新攝影機顯示影像或倒數計時器等等應用。

以下的 Python PyQt5 QTimer 用法與範例將分為這幾部分,

  • PyQt5 QTimer 基本用法與綁定事件
  • PyQt5 用按鈕事件啟動 QTimer 定時器
  • PyQt5 QTimer 倒數計時器

PyQt5 QTimer 基本用法與綁定事件

這邊示範 PyQt5 QTimer 基本用法,這邊先示範一個定時器搭配一個 label,每間隔 1 秒就更新秒數在 label 上,

QTimer 綁定事件可以用 QTimer.timeout.connect() 這個信號,將信號綁訂到對應處理的函式,如下例中的 onTimer(),當時間到時會執行 onTimer()

要啟動 QTimer 定時器的話要使用 QTimer.start(),引數是帶入時間間隔,單位為 ms 毫秒,例如帶入 1000 表示每隔 1000ms 毫秒會觸發 onTimer() 一次,因此不僅僅只是觸發一次而已。需要停止 QTimer 可以使用 QTimer.stop() ,在下一個範例會介紹到,

python-pyqt-qtimer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QLabel)
from PyQt5.QtGui import QFont
from PyQt5.QtCore import QTimer

class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.setWindowTitle('my window')
self.setGeometry(50, 50, 200, 150)

self.mylabel = QLabel('timer', self)
self.mylabel.setFont(QFont('Arial', 24))
self.mylabel.move(60, 50)

self.counter = 0

self.mytimer = QTimer(self)
self.mytimer.timeout.connect(self.onTimer)
self.mytimer.start(1000)

def onTimer(self):
self.counter += 1
self.mylabel.setText(str(self.counter))

if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())

結果圖如下,

PyQt5 用按鈕事件啟動 QTimer 定時器

這邊介紹 PyQt5 用按鈕事件啟動 QTimer 定時器,利用一個按鈕啟動 QTimer 定時器,另一個按鈕來停止 QTimer 定時器。

將 mybutton1 的按下事件綁定 startTimer() 函式,startTimer() 會使用 QTimer.start() 去啟動 timer 跟其它相關的設定,接著將 mybutton2 的按下事件綁定 stopTimer() 函式,stopTimer() 會使用 QTimer.stop() 去停止 timer。

這邊的 mybutton1 與 mybutton2 按鈕還加上了防呆設計,當 mybutton1 按下時,mybutton1 就必須 disable,避免使用者重複按下,而 mybutton2 要 enable,而按下 mybutton2 時則反之,

python-pyqt-qtimer2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QGridLayout, QLabel, QPushButton)
from PyQt5.QtGui import QFont
from PyQt5.QtCore import QTimer

class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.setWindowTitle('my window')
self.setGeometry(50, 50, 200, 150)

gridlayout = QGridLayout()
self.setLayout(gridlayout)

self.mylabel = QLabel('timer', self)
self.mylabel.setFont(QFont('Arial', 24))
gridlayout.addWidget(self.mylabel, 0, 0, 1, 2)

self.mybutton1 = QPushButton('start', self)
self.mybutton1.clicked.connect(self.startTimer)
gridlayout.addWidget(self.mybutton1, 1, 0)

self.mybutton2 = QPushButton('stop', self)
self.mybutton2.clicked.connect(self.stopTimer)
self.mybutton2.setDisabled(True)
gridlayout.addWidget(self.mybutton2, 1, 1)

self.mytimer = QTimer(self)
self.mytimer.timeout.connect(self.onTimer)

def startTimer(self):
self.counter = 0
self.mylabel.setText('start timer...')
self.mybutton1.setDisabled(True)
self.mybutton2.setDisabled(False)
self.mytimer.start(1000)

def stopTimer(self):
self.mylabel.setText('stop timer')
self.mybutton1.setDisabled(False)
self.mybutton2.setDisabled(True)
self.mytimer.stop()

def onTimer(self):
self.counter += 1
self.mylabel.setText(str(self.counter))

if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())

結果圖如下,這邊是使用 QGridLayout 來進行 UI 元件排版,更多的 QGridLayout 介紹可以參考Python PyQt5 QGridLayout 網格佈局用法與範例這篇,

以上的範例也可以把兩個按鈕精簡成一個按鈕,第一次按下按鈕時啟動定時器,再次按下按鈕就停止計時器,這邊就先簡單介紹不作示範了。

PyQt5 QTimer 倒數計時器

QTimer 除了作定時器以外,你也可以利用 QTimer 來實作倒數計時器,倒數完後停止定時器後去執行你想要的函式或程式邏輯,以下示範倒數 5 秒後執行一行 print()

python-pyqt-qtimer3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QLabel)
from PyQt5.QtGui import QFont
from PyQt5.QtCore import QTimer

class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.setWindowTitle('my window')
self.setGeometry(50, 50, 200, 150)

self.counter = 5
self.mylabel = QLabel(str(self.counter), self)
self.mylabel.setFont(QFont('Arial', 24))
self.mylabel.move(60, 50)

self.mytimer = QTimer(self)
self.mytimer.timeout.connect(self.onTimer)
self.mytimer.start(1000)

def onTimer(self):
self.counter -= 1
self.mylabel.setText(str(self.counter))
if self.counter == 0:
self.mytimer.stop()
print('time out')

if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())

結果圖如下,

以上就是 Python PyQt5 QTimer 用法與範例介紹,
如果你覺得我的文章寫得不錯、對你有幫助的話記得 Facebook 按讚支持一下!

其它相關文章推薦
Python 新手入門教學懶人包
Python PyQt5 新手入門教學