PyQt Progressbar QThread not working when renderin

2019-08-16 11:03发布

I looked at this answer here which was very helpful, but it's not working as expected when trying to render a plot.

The plot is working but the progress bar is not progressing, it will just jump to 100% after the plot is rendered. Of course, I would like the progress bar to be progressing while the plot is rendering, not after it's finished.

I think, I might be missing a PyQt function to somehow connect the event or the pyqt signal, but actually I have no idea how to fix this.

Here is, what I hope to be, a Minimal, Complete, and Verifiable example:

import sys
from pandas.tools.plotting import scatter_matrix
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import time

from PyQt5.QtWidgets import (QWidget, QProgressBar, 
    QPushButton, QApplication, QVBoxLayout)
from PyQt5 import QtCore, QtGui

class MyCustomWidget(QWidget):

    def __init__(self, parent=None):
        super(MyCustomWidget, self).__init__(parent)
        layout = QVBoxLayout(self)

        self.progressBar = QProgressBar(self)
        self.progressBar.setRange(0,1)
        layout.addWidget(self.progressBar)
        self.count = 0
        button = QPushButton("Start", self)
        layout.addWidget(button)
        self.df = pd.DataFrame(np.random.rand(3, 12))

        button.clicked.connect(self.onStart)

        self.myLongTask = TaskThread()
        self.myLongTask.taskFinished.connect(self.onFinished)

        self.show()

    def onStart(self):
        scatter_matrix(self.df, alpha=0.8, figsize=(7, 7), diagonal='kde')
        self.scatter = plt
        self.scatter.suptitle('Data Scatter Matrix Plot', size=16)
        self.progressBar.setRange(0,0)
        self.progressBar.setValue(1)
        self.myLongTask.start()
        self.scatter.show()

    def onFinished(self):
        self.progressBar.setRange(0,1)


class TaskThread(QtCore.QThread):
    taskFinished = QtCore.pyqtSignal()
    def run(self):
        time.sleep(3)
        self.taskFinished.emit()

if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = MyCustomWidget()
    sys.exit(app.exec_())

0条回答
登录 后发表回答