上一篇簡單的應用了 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_())
訂閱:
張貼留言 (Atom)
ChartGPT 學 python 很強喔
ChartGPT 最近很紅 , 前一陣子有一些小 module 本來想 google 一下 語法 但發現用 chartGPT 直接請他給 sample code 反而較快 所以只要你有基本 知識 ChartGPT 可以加速你的開發 好用喔
-
之前安裝樹莓常用軟體 如 anydesk or teamviewr , or pycharm 等 都是自己下載 source compile .. 但遇到了不少坑 , 每次都要花不少時間 如 obs 有時能成功, 但大都失敗 直到昨天看到有人做好了 script 只要執...
-
基本運算 x= 99 print(x) print(x + 1 ) # 加法 print(x - 1 ) # 減法: print(x * 2 ) # 乘法: print(x / 2 ) # 除法: print(x // 2 ) # 整數除法 p...
-
安裝 pysimplegui 很簡單 pip install pysimplegui or pip3 install pysimplegui 即可 Linux, Mac, Windows 樹莓派 都支援 先說一下 pysimplegui 與其他 GUI 如 pyqt5 等...
沒有留言:
張貼留言