上一篇簡單的應用了 QTimer 來做計時器, 再來我們增加兩了 button : Start , Stop
當按Start 開始計數, 和下stop 就停止
很簡單我們只要把 timer.start 與 timer.stop 加入 button 的 slot function 中即可
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QGridLayout, QWidget,QPushButton
from PyQt5.QtGui import QPixmap, QImage,QFont
from PyQt5.QtCore import QTimer
class window(QWidget):
def __init__(self):
super().__init__()
self.label = QLabel(self)
self.label.setText("0")
self.label.setGeometry(300,100,200,200)
self.label.setFont(QFont("Roman times",100,QFont.Bold))
self.setGeometry(500,300,700,500)
self.setWindowTitle("PyQT Timer Demo")
self.timer=QTimer(self)
self.timer.timeout.connect(self.run)
# self.timer.start(1000)
self.total = 0
#add start Btn
self.startBtn = QPushButton(self)
self.stopBtn = QPushButton(self)
self.startBtn.clicked.connect(self.startCount)
self.startBtn.setGeometry(50,400,100,50)
self.startBtn.setText("Start")
self.stopBtn.clicked.connect(self.stopCount)
self.stopBtn.setGeometry(500,400,100,50)
self.stopBtn.setText("Stop")
#Start the Timer
def startCount(self):
self.timer.start(1000)
# Stop the Timer
def stopCount(self):
self.timer.stop()
def run(self):
self.label.setText(str(self.total))
self.total+=1
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = window()
ex.show()
sys.exit(app.exec_())
2019年1月30日 星期三
訂閱:
張貼留言 (Atom)
Bloomberg Markets Wrap 2025-08-15: 股市因美國數據下漲,川普與普丁會議獲關注
Bloomberg Markets Wrap 2025-08-15: 股市因美國數據下滑,川普與普丁會談受關注 發佈日期:2025-08-15 本篇文章翻譯與整理自 Bloomberg Markets Wrap(經 SWI swissin fo.ch 彙整),該報111導回顧了...
-
Timer 是 UI 很重要的一個功能, 因為UI 常常會因為時間定期自己更新畫面, 如camera camera 可以想像成 每幾個 ms 會產生一個圖案 (image) 所以需要每幾個 ms 去處理一次畫面 另外一個更常用的是計時器, 下面我先demo...
-
安裝 pysimplegui 很簡單 pip install pysimplegui or pip3 install pysimplegui 即可 Linux, Mac, Windows 樹莓派 都支援 先說一下 pysimplegui 與其他 GUI 如 pyqt5 等...
-
imagezmq 套件 讓人輕易的可以將遠端的影像串流傳送到server 上 目前有一個專案 , 需要將 Tx2 上處理過的影像傳到 座位上以方便監控 使用 imagezmq 開發讓事情變得很間單 架構如下 Tx2 (imagezmq client) --...
沒有留言:
張貼留言