Unsolved How can i stop the progressbar while loop with the pushbutton? (Pyqt5 Gui)
This is a basic progress bar.
i just tried this code. the progress bar is loading but not stopping when i push the stop button it just contiues.
and whenever i push the stop button the application is locking until progress bar finish.
import sys import time from PyQt5.QtWidgets import (QApplication, QDialog, QProgressBar, QPushButton) TIME_LIMIT = 100 class Actions(QDialog): """ Simple dialog that consists of a Progress Bar and a Button. Clicking on the button results in the start of a timer and updates the progress bar. """ def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle('Progress Bar') self.progress = QProgressBar(self) self.progress.setGeometry(0, 0, 300, 100) self.progress.setMaximum(100) self.button = QPushButton('Start', self) self.button.move(0, 30) self.button2 = QPushButton('Stop', self) self.button2.move(30, 60) self.show() self.show() self.button.clicked.connect(self.onButtonClick) self.button2.clicked.connect(self.onButtonClick2) def onButtonClick(self): count = 0 while count < TIME_LIMIT: count += 1 time.sleep(1) self.progress.setValue(count) print(count) stop = 0 if stop == 1: break def onButtonClick2(self): stop = 1 if __name__ == "__main__": app = QApplication(sys.argv) window = Actions() sys.exit(app.exec_())
while count < TIME_LIMIT:
count += 1
Such code should never be in the main thread of an event driven application! This is a no go.
It blocks the main thread, so the event loop is blocked until the loop terminates.
Remove the while loop and use a QTimer to update the progress dialog.
JonB last edited by JonB
stop = 0 if stop == 1: break def onButtonClick2(self): stop = 1
How do you expect
== 1so as to
breakout of your loop? Conceptually,
onButtonClick2()would have to get called immediately after
stop = 0and before
if stop == 1, which ain't going to happen. Your
stop = 0needs to be moved outside the loop which tests its value.
Plus, in Python those two occurrences of a
stopvariable in two separate functions (
onButtonClick2) are two separate local variables, and one does not refer to the other. You would need to make them
self.stopfor it to begin to work. Just saying....