上一篇簡單的應用了 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導回顧了...
-
PyQT 為何很多人用了開發 UI , 一個原因是因為 它包含了很多實用的 Widget , Calendar 就是其中一個 , 這省了很多開發時間, 不然自己寫一個日曆 是會瘋掉的 Calendar 物件用法跟前面的 button 差不多, 但多了一些日期的處理 下面這...
-
現在基本上所有設備都需要無線網路上網 此次朋友家整棟 3層 都要使用無線網路 之前自己家是使用 PLC 來連 , 但 朋友家太老舊了 使用 PLC 可能速度不會很高 於是想說試試 Mesh AP 此次 pchome D-Link 有優惠 , 就買 3 台 Cov...
-
之前安裝樹莓常用軟體 如 anydesk or teamviewr , or pycharm 等 都是自己下載 source compile .. 但遇到了不少坑 , 每次都要花不少時間 如 obs 有時能成功, 但大都失敗 直到昨天看到有人做好了 script 只要執...

沒有留言:
張貼留言