在使用按钮循环时如何停止进度栏?

时间:2019-11-25 01:22:07

标签: python pyqt pyqt5

这是基本的进度条。

我刚刚尝试了这段代码。进度条正在加载,但是当我按下“停止”按钮时它并没有停止。

每当我按下“停止”按钮时,应用程序就会锁定,直到进度条结束。

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_())

1 个答案:

答案 0 :(得分:0)

在您的示例中,stop方法中的onButtonClick()变量 和stop方法中的onButtonClick2()变量是不同的局部变量。

while循环,而sleep(1)功能阻止主界面。

上面的任务可能看起来像这样:

import sys
from PyQt5.QtWidgets import (QApplication, QDialog, QProgressBar, QPushButton)
from PyQt5.QtCore import QThread, pyqtSignal


class Thread(QThread):
    update_signal = pyqtSignal(int) 

    def __init__(self, *args, **kwargs):
        super(Thread, self).__init__(*args, **kwargs)
        self.count   = 0
        self.running = True

    def run(self):
        while self.running and self.count < 100:
            self.count += 1
            self.update_signal.emit(self.count)
            QThread.msleep(100)                   

    def stop(self):
        self.running = False


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.progress.setValue(0)

        self.button = QPushButton('Start', self)
        self.button.move(0, 30)
        self.button2 = QPushButton('Stop', self)
        self.button2.setEnabled(False)
        self.button2.move(30, 60)

        self.button.clicked.connect(self.onButtonClick)
        self.button2.clicked.connect(self.on_stop)

        self.thread = Thread()
        self.thread.update_signal.connect(self.update)

    def onButtonClick(self):
        self.button2.setEnabled(True)
        self.progress.setValue(0)
        self.thread.running = True
        self.thread.count = 0
        self.thread.start()
        self.button.setEnabled(False)

    def update(self, val):
        self.progress.setValue(val)
        if val == 100: self.on_stop()

    def on_stop(self):
        self.thread.stop()
        self.button.setEnabled(True)
        self.button2.setEnabled(False)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Actions()
    window.show()
    sys.exit(app.exec_())

enter image description here