我查看了这个答案here,这非常有帮助,但在尝试渲染图时,它没有按预期工作。
情节正在运行,但进度条不是正在进行,它会在渲染绘图后跳转到100%。当然,我希望进度条在绘图时呈现进展,而不是在完成后。
我想,我可能会错过一个PyQt函数以某种方式连接事件或pyqt信号,但实际上我不知道如何解决这个问题。
这是我希望成为的,一个最小的,完整的,可验证的例子:
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_())