上一篇簡單的應用了 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)
ChartGPT 學 python 很強喔
ChartGPT 最近很紅 , 前一陣子有一些小 module 本來想 google 一下 語法 但發現用 chartGPT 直接請他給 sample code 反而較快 所以只要你有基本 知識 ChartGPT 可以加速你的開發 好用喔
-
我的樹莓派 網路 突然不能用 摻考網路 restart network service 就 ok 了 pi@raspberrypi4:~ $ sudo mii-tool -r eth0 restarting autonegotiation... pi@r...
-
按鈕(Buttons) 是 GUI 中很常用到的元件 按鈕的建置跟上一篇提到很相識, 但多了一個連動的概念以 PyQT 來說就是Signals and Slot (訊號與槽) 葉就是說當我按了一個 Button ( 發射一個訊號) 要執行哪個動作 (到哪個槽) ...
-
最近需要將 Raspberry Pi 收集的資料 統計給別人看 本來是用 GCP 自己收集資料 , 自己寫網頁 但 GCP 費用也不便宜 survey 一下 thing speaker 平台是最簡的單 直接用 https get 就可以將資料上傳到他們的平台了
沒有留言:
張貼留言