get paid

Get paid for the tasks you do online

2019年1月30日 星期三

PyQT5教學 QTimer 應用計時器

上一篇簡單的應用了 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_())

沒有留言:

張貼留言

ChartGPT 學 python 很強喔

 ChartGPT 最近很紅 ,  前一陣子有一些小 module  本來想 google  一下 語法 但發現用 chartGPT 直接請他給 sample code 反而較快 所以只要你有基本 知識 ChartGPT 可以加速你的開發 好用喔