PyQt Progressbar QThread not working when rendering a graph

空扰寡人 提交于 2019-12-11 14:22:55

问题


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

来源:https://stackoverflow.com/questions/47256606/pyqt-progressbar-qthread-not-working-when-rendering-a-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!