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_())
-
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_())
@caca91 said in How can i stop the progressbar while loop with the pushbutton? (Pyqt5 Gui):
while count < TIME_LIMIT:
count += 1
time.sleep(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. -
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_())
@caca91
As @jsulm has written, your main issue is that you must not usesleep()
and you must start using Qt/PySide2 signals/slots, like aQTimer
.However, additionally:
stop = 0 if stop == 1: break def onButtonClick2(self): stop = 1
How do you expect
stop
to ever== 1
so as tobreak
out of your loop? Conceptually,onButtonClick2()
would have to get called immediately afterstop = 0
and beforeif stop == 1
, which ain't going to happen. Yourstop = 0
needs to be moved outside the loop which tests its value.Plus, in Python those two occurrences of a
stop
variable in two separate functions (onButtonClick
&onButtonClick2
) are two separate local variables, and one does not refer to the other. You would need to make themself.stop
for it to begin to work. Just saying....