QTimer 对象在接近尾声时随机停止,它不会在同一点左右停止,我不明白为什么它同时停止了自定义小部件和变量本身,所以不是小部件无法显示它。
import sys
from PySide2.QtWidgets import QWidget, QSlider, QLabel, QProgressBar, QPushButton, QHBoxLayout, QVBoxLayout, QApplication
from PyQt5.QtCore import QTimer,Qt
from PySide2extn.RoundProgressBar import roundProgressBar
class Timer(QWidget):
def __init__(self):
super().__init__()
# Create a slider to set the timer duration
self.slider = QSlider()
self.slider.setOrientation(Qt.Horizontal) # vertical slider
self.slider.setRange(1, 60) # timer duration range: 1-60 seconds
self.slider.setValue(10) # default timer duration: 10 seconds
self.slider.valueChanged.connect(self.update_label)
self.time=0
# Create a label to display the current timer duration
self.label = QLabel(f'Timer duration: {self.slider.value()} seconds')
# Create a progress bar to show the timer progress
self.progress = roundProgressBar()
self.progress.rpb_setRange(0, self.slider.value() * 1000)
self.progress.rpb_setValue(0)
# Create start, stop, and reset buttons
self.start_button = QPushButton('Start')
self.stop_button = QPushButton('Stop')
self.reset_button = QPushButton('Reset')
self.start_button.clicked.connect(self.start_timer)
self.stop_button.clicked.connect(self.stop_timer)
self.reset_button.clicked.connect(self.reset_timer)
self.stop_button.setEnabled(False)
self.reset_button.setEnabled(False)
# Create a layout for the buttons
button_layout = QHBoxLayout()
button_layout.addWidget(self.start_button)
button_layout.addWidget(self.stop_button)
button_layout.addWidget(self.reset_button)
# Create a layout and add the slider, label, progress bar, and buttons
layout = QVBoxLayout()
layout.addWidget(self.slider)
layout.addWidget(self.label)
layout.addWidget(self.progress)
layout.addLayout(button_layout)
self.setLayout(layout)
# Create a timer and set the timeout signal
self.timer = QTimer()
self.timer.timeout.connect(self.timeout)
self.progress_timer = QTimer()
self.progress_timer.timeout.connect(self.update_progress)
def update_label(self):
self.label.setText(f'Timer duration: {self.slider.value()} seconds')
self.progress.rpb_setRange(0, self.slider.value() * 1000)
def update_progress(self):
self.time+=100
print(self.time)
self.progress.rpb_setValue(self.time)
def start_timer(self):
# Set the timer duration in seconds
duration = self.slider.value()
# Start the timer and progress timer
self.timer.start(duration * 1000) # milliseconds
self.progress_timer.start(100) # 100ms interval
# Disable the start button, enable the stop and reset buttons
self.start_button.setEnabled(False)
self.stop_button.setEnabled(True)
self.reset_button.setEnabled(True)
self.slider.setEnabled(False)
def stop_timer(self):
# Stop the timer and progress timer
self.timer.stop()
self.progress_timer.stop()
# Disable the stop button, enable the start button
self.stop_button.setEnabled(False)
self.start_button.setEnabled(True)
self.slider.setEnabled(True)
def reset_timer(self):
# Stop the timer and progress timer
self.time=0
self.timer.stop()
self.progress_timer.stop()
# Reset the progress bar to 0, enable the start button, disable the stop and reset buttons
self.progress.rpb_setValue(0)
self.start_button.setEnabled(True)
self.stop_button.setEnabled(False)
self.reset_button.setEnabled(False)
def timeout(self):
# Stop the timer and progress timer
self.timer.stop()
self.progress_timer.stop()
# Disable the stop button, enable the start button and reset button
self.stop_button.setEnabled(False)
if __name__ == '__main__':
app = QApplication(sys.argv)
timer = Timer()
timer.show()
sys.exit(app.exec_())
感谢您的帮助。我已经尝试过 PyQt5 和 PySide2 QTImer,尽管它们应该是相同的,但都不起作用。
你计时的方式不准确。 您可以通过进行以下更改来证明这一点:
def update_progress(self):
self.time += 100
duration = self.slider.value() * 1000
elapsed_time = duration - self.timer.remainingTime()
print(self.time, elapsed_time)
self.progress.setValue(elapsed_time)
在打印输出中,您可以看到
self.time
和 elapsed_time
不同步。 self.time
落后,给人的印象是计时器过早停止,而实际上它确实用完了。使用elapsed_time
作为设置进度条的值将给出计时器的准确进度。
它仍然不会达到 100%,因为计时器超时的时刻将在更新之间进行。如果您降低更新间隔,您将接近 100%。