from functools import partial
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from timer_clock import *
class TimerClock(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(TimerClock, self).__init__()
self.setupUi(self)
self.statusBar().showMessage("Ready")
# status_running表示是否在计时状态中
# 0 初始态; 1 运行态; 2 暂停态
self.status_running = 0
self.counter = QTime()
self.timer = QTimer()
# 固定QTimer的轮询参数,指定轮询间隔为5ms
self.timer_start = partial(self.timer.start, 5)
self.elasped_time = 0
self.target_time = None
self.timer.timeout.connect(self.timer_timeout)
# 开始/暂停按钮功能实现
def click_start_button(self):
if self.status_running == 0:
self.target_time = int(self.spinBox.text()) * 60
self.target_time = None if self.target_time == 0 else self.target_time
self.status_running = 1
self.pushButton.setText("暂停")
self.counter.restart()
self.timer_start() # 开始计时
elif self.status_running == 1:
self.status_running = 2
self.pushButton.setText("继续")
self.elasped_time = self.elasped_time + self.counter.elapsed() // 1000
if self.target_time is not None:
self.target_time -= self.counter.elapsed() // 1000
self.timer.stop()
else:
self.status_running = 1
self.pushButton.setText("暂停")
self.counter.start()
self.timer_start() # 开始计时
# 重置按钮功能实现
def click_reset_button(self):
# 停止循环和计时
self.timer.stop()
#self.counter.restart()
# 参数重置,组件状态重置
self.status_running = 0
self.target_time = None
self.elasped_time = 0
self.pushButton.setText("开始")
self.lineEdit.setText("00:{:02}:00".format(int(self.spinBox.text())))
# 计时器轮询
def timer_timeout(self):
t_secs = self.counter.elapsed()//1000
if self.target_time is not None:
t_secs = self.target_time - t_secs
else:
t_secs += self.elasped_time
# 计时结束
if t_secs < 0:
msBox = QMessageBox(QMessageBox.NoIcon, "提示", "计时结束!")
msBox.exec_()
self.timer.stop()
self.pushButton.setText("开始")
t_str = "{:02}:{:02}:{:02}".format((t_secs//60)//60,(t_secs//60)%60, (t_secs)%60)
self.lineEdit.setText(t_str)
# spinbox值改变
def spinBox_valuechanged(self, value):
self.lineEdit.setText("00:{:02}:00".format(int(self.spinBox.text())))
def show_help_message(self):
msBox = QMessageBox(QMessageBox.NoIcon, "关于", "计时器\r\n Version:0.1\r\n 作者:SSS")
msBox.exec_()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
ui = TimerClock()
ui.show()
sys.exit(app.exec_())