简体   繁体   English

使用 QProgressbar 时出错 PyQt5 python

[英]error while using QProgressbar PyQt5 python

i have the following code in which I implemented a QprogressDialog it works fine the first time I press the Start button to run the create function but if the dialog finishes and I try to run it again it shows an empty bar that does not increases the progress.我有以下代码,其中我实现了 QprogressDialog 它在我第一次按下开始按钮运行创建 function 时工作正常但是如果对话框完成并且我尝试再次运行它它会显示一个空栏,不会增加进度. any ideas?有任何想法吗?

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
import time


class Worker(QObject):
    signal = pyqtSignal(int)

    def __init__(self):
        super().__init__()
    @pyqtSlot()
    def do_work(self) -> None:
        for value in range(1,101):
            self.signal.emit(value)
            time.sleep(0.01)
        

class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.layout = QVBoxLayout()
        self.con = QWidget()
        self.setCentralWidget(self.con)
        self.con.setLayout(self.layout)
        self.btn = QPushButton("Start",self)
        self.layout.addWidget(self.btn)
        self.thread = QThread()
        self.thread.finished.connect(self.thread_finished)
        self.btn.pressed.connect(self.create)


    def create(self):
        self.p = QProgressDialog("Names", "Cancel", 0, 100)
        self.p.setValue(0)
        self.p.setWindowModality(Qt.ApplicationModal)
        self.p.canceled.connect(self.p.deleteLater)
        self.p.show()
        self.worker = Worker()
        self.worker.signal.connect(self.p.setValue)
        self.worker.moveToThread(self.thread)
        self.thread.started.connect(self.worker.do_work)
        self.thread.start()

        
        
    def thread_finished(self):
        self.thread.deletelater()
        # self.thread = QThread()
        # self.thread.finished.connect(self.thread_finished)



app = QApplication(sys.argv)
window = Window()
window.show()
app.exec_()

i tried making the dialog a local variable but it does not work either我尝试将对话框设为局部变量,但它也不起作用

When do_work finishes, it just returns, but the thread is still alive, so nothing happens when you call again start() :do_work完成时,它只是返回,但线程仍然存在,所以当您再次调用start()时没有任何反应:

If the thread is already running, this function does nothing.如果线程已在运行,则此 function 不执行任何操作。

Besides, deleting and recreating the dialog or the thread is pointless.此外,删除并重新创建对话框或线程是没有意义的。 At most, you need to recreate the worker (assuming it has some instance attributes that will not be used again if restarted), but the most important part is that you have to tell the thread to quit() when your function ends, using a signal in the worker object.至多,你需要重新创建工作者(假设它有一些实例属性,如果重新启动将不会再次使用),但最重要的部分是你必须告诉线程quit()当你的 function 结束时,使用在工人 object 中发出信号。

class Worker(QObject):
    signal = pyqtSignal(int)
    finished = pyqtSignal()

    @pyqtSlot()
    def do_work(self) -> None:
        self.keepRunning = True
        for value in range(1,101):
            if not self.keepRunning:
                break
            self.signal.emit(value)
            time.sleep(0.01)
        self.finished.emit()

    def stop(self):
        self.keepRunning = False


class Window(QMainWindow):
    def __init__(self):
        # ...
        self.p = QProgressDialog("Names", "Cancel", 0, 100)
        self.p.setWindowModality(Qt.ApplicationModal)

    def create(self):
        self.p.show()

        self.worker = Worker()
        self.worker.signal.connect(self.p.setValue)
        self.p.canceled.connect(self.worker.stop)

        self.worker.moveToThread(self.thread)
        self.worker.finished.connect(self.thread.quit)

        self.thread.started.connect(self.worker.do_work)
        self.thread.finished.connect(self.worker.deleteLater)
        self.thread.start()

Note: what you had was a problem, not an error.注意:您遇到的是问题,而不是错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM