Issue
The QTimer object stops randomly near the end, it does not stop at the same point just around, I cannot see why as it stop both the custom widget and the variable itself, so it is not the widget cannot display it.
import sys
from PySide2.QtWidgets import QWidget, QSlider, QLabel, QProgressBar, QPushButton, QHBoxLayout, QVBoxLayout, QApplication
from PySide2.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_())
Thanks for any help.
DONT NOT MIX PYSIDE2 AND PYQT5. Refer to the first comment on the question as to why.
Solution
The way you're keeping time is not accurate. You can demonstrate this by making these changes:
def update_progress(self):
self.time += 100
elapsed_time = self.timer.interval() - self.timer.remainingTime()
print(self.time, elapsed_time)
self.progress.setValue(elapsed_time)
In the print output you can see that self.time
and elapsed_time
are not in sync. self.time
falls behind and that gives the impression that the timer stops prematurely while in fact, it does run out. Using the elapsed_time
as value to set the progress bar will give the accurate progress of the timer.
It will still not reach 100% though because the moment the timer times out will fall between updates. If you lower the update interval you will get closer to 100%.
Answered By - mahkitah
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.