PyQt5倒计时(5分钟)

from PyQt5.QtWidgets import QApplication, QMessageBox, QHBoxLayout, QVBoxLayout, QPushButton, QSpinBox, QLCDNumber, QWidget
from PyQt5.QtCore import QThread, pyqtSignal
import sys

class TimerThread(QThread):
    startTimer = pyqtSignal()
    endTimer = pyqtSignal()

    def run(self):
        while True:
            self.sleep(1)
            if totalCurrent == 0:
                self.endTimer.emit()   #倒计时时间到
                break
            self.startTimer.emit()

class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('倒计时')
        self.resize(500, 420)
        self.layoutMain = QVBoxLayout()

        self.lcdnumber = QLCDNumber()
        self.lcdnumber.display("05:00")
        self.spinbox = QSpinBox()
        self.spinbox.setRange(1, 20)
        self.spinbox.setSingleStep(1)
        self.spinbox.setValue(5)
        self.spinbox.valueChanged.connect(self.value_change_func)
        self.button = QPushButton('开始')
        self.button1 = QPushButton("停止")

        self.layoutMain.addWidget(self.lcdnumber)
        self.layout1 = QHBoxLayout()
        self.layout1.addWidget((self.spinbox))
        self.layout1.addWidget(self.button)
        self.layout1.addWidget(self.button1)
        self.layoutMain.addLayout(self.layout1)

        self.timerThread = TimerThread()
        self.timerThread.startTimer.connect(self.startTimer)
        self.timerThread.endTimer.connect(self.endTimer)
        self.button.clicked.connect(self.workOn)
        self.button1.clicked.connect(self.stopTimer)

        self.setLayout(self.layoutMain)

    def value_change_func(self):
        global totalCurrent
        totalCurrent = self.spinbox.value() * 60
        totalNum = ("00" + str(self.spinbox.value()))[-2:] + ":00"
        self.lcdnumber.display(totalNum)
        self.timerThread.terminate()

    def workOn(self):
        global totalCurrent
        totalCurrent = self.spinbox.value() * 60
        self.timerThread.start()
        print(self.spinbox.value())

    def startTimer(self):
        global totalCurrent
        totalCurrent -= 1
        curMinu = ("00" + str(totalCurrent // 60))[-2:]
        curSec = ("00" + str(totalCurrent % 60))[-2:]
        self.lcdnumber.display(curMinu + ":" + curSec)

    def endTimer(self):
        QMessageBox.information(self, '提醒', '时间到', QMessageBox.Ok)

    def stopTimer(self):
        self.lcdnumber.display("05:00")
        self.spinbox.setValue(5)
        self.timerThread.terminate()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    mainWindow.show()
    sys.exit(app.exec())

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值