如何在按下按钮时停止progressbar?

2024-04-26 23:33:00 发布

您现在位置:Python中文网/ 问答频道 /正文

这是一个基本的进度条

我刚试过这个密码。进度条正在加载,但没有停止当我按下停止按钮它只是继续

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

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

Tags: the进度条importselftimedefcountsys
1条回答
网友
1楼 · 发布于 2024-04-26 23:33:00

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

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

相关问题 更多 >