PyQt5计时器点击重置按钮无法归零问题求助
问题分析
你的计时器重置功能失效的核心问题在于时间计算依赖的起始时间从未被正确重置:
- 你定义了全局变量
start = datetime.datetime.now(),这个值是程序启动时的固定时间,后续点击Start、Reset按钮都没有更新它。 - 点击Reset时,你只重置了显示的
self.count和暂停标记self.flag,但当再次点击Start时,showTime方法依旧用程序启动时的start时间来计算流逝时间,导致计时器从之前累计的时长继续走。 - 另外,Start按钮只设置了
self.flag = True,没有在每次启动时记录新的起始时间,这会导致暂停后再启动,时间会跳过暂停的时长,直接从程序启动的总流逝时间计算,这也是一个潜在问题。
修复方案
把起始时间改成类的实例变量,在Start时记录当前时间,Reset时重置所有计时相关变量:
# importing libraries from PyQt5.QtWidgets import * from PyQt5 import QtCore, QtGui from PyQt5.QtGui import * from PyQt5.QtCore import * import sys import datetime class Window(QMainWindow): def __init__(self): super().__init__() self.setWindowFlags(QtCore.Qt.FramelessWindowHint) self.setAttribute(QtCore.Qt.WA_TranslucentBackground) # setting title self.setWindowTitle("Python Stop watch") # setting geometry self.setGeometry(100, 100, 400, 500) # calling method self.UiComponents() # showing all the widgets self.show() # method for widgets def UiComponents(self): # counter self.count = '{:02d}:{:02d}:{:02d}'.format(0,0,0) # 初始化实例变量的起始时间 self.start_time = datetime.datetime.now() # 记录暂停时已经流逝的秒数 self.elapsed_paused = 0 # creating flag self.flag = False # creating a label to show the time self.label = QLabel(self) # setting geometry of label self.label.setGeometry(75, 100, 250, 70) # adding border to the label self.label.setStyleSheet("border : 4px solid black;") # setting text to the label self.label.setText(str(self.count)) # setting font to the label self.label.setFont(QFont('Arial', 25)) # setting alignment to the text of label self.label.setAlignment(Qt.AlignCenter) # creating start button start = QPushButton("Start", self) # setting geometry to the button start.setGeometry(125, 250, 150, 40) # add action to the method start.pressed.connect(self.Start) # creating pause button pause = QPushButton("Pause", self) # setting geometry to the button pause.setGeometry(125, 300, 150, 40) # add action to the method pause.pressed.connect(self.Pause) # creating reset button re_set = QPushButton("Re-set", self) # setting geometry to the button re_set.setGeometry(125, 350, 150, 40) # add action to the method re_set.pressed.connect(self.Re_set) # creating a timer object timer = QTimer(self) # adding action to timer timer.timeout.connect(self.showTime) # update the timer every tenth second timer.start(100) # method called by timer def showTime(self): # checking if flag is true if self.flag: # 计算当前流逝的总时间:暂停前的时长 + 本次启动后的时长 current_elapsed = (datetime.datetime.now() - self.start_time).total_seconds() + self.elapsed_paused self.hour = int(current_elapsed // 3600) self.min = int(current_elapsed % 3600 // 60) self.seconds = int(current_elapsed % 60) self.count ='{:02d}:{:02d}:{:02d}'.format(self.hour, self.min, self.seconds) # getting text from count text = str(self.count) # showing text self.label.setText(text) def Start(self): # 启动时记录当前时间作为新的起始点 self.start_time = datetime.datetime.now() # making flag to true self.flag = True def Pause(self): # 暂停时记录已经流逝的总时长 if self.flag: self.elapsed_paused += (datetime.datetime.now() - self.start_time).total_seconds() # making flag to False self.flag = False def Re_set(self): # making flag to false self.flag = False # 重置暂停累计时长 self.elapsed_paused = 0 # reseeting the count self.count = '{:02d}:{:02d}:{:02d}'.format(0,0,0) # 重置起始时间为当前时间(避免后续启动时计算错误) self.start_time = datetime.datetime.now() # setting text to label self.label.setText(str(self.count)) # create pyqt5 app App = QApplication(sys.argv) # create the instance of our Window window = Window() # start the app sys.exit(App.exec())
关键修改点
- 移除全局
start变量,改用类实例变量self.start_time和self.elapsed_paused来管理计时状态 - Start按钮点击时,重新记录当前时间作为新的计时起始点
- Pause按钮点击时,累计已经流逝的时长,确保暂停后再启动能继续计时
- Reset按钮点击时,重置所有计时相关变量:暂停累计时长、起始时间、显示计数和标记
内容的提问来源于stack exchange,提问作者Abhishek Shahi
相关产品推荐
相关产品推荐

