Python 使用Matplotlib在GUI中从单独的线程保存图形

Python 使用Matplotlib在GUI中从单独的线程保存图形,python,multithreading,matplotlib,user-interface,pyside,Python,Multithreading,Matplotlib,User Interface,Pyside,我使用一个QRunnable类从gui中启动一个单独的线程(用于非阻塞功能)。此线程使用Matplotlib将一些绘图保存为PDF。然而,它给出了一些警告 class Results(QRunnable): def __init__(self): super(Results, self).__init__() def run(self): plt.plot([1,2,3,4],[1,2,3,4])

我使用一个QRunnable类从gui中启动一个单独的线程(用于非阻塞功能)。此线程使用Matplotlib将一些绘图保存为PDF。然而,它给出了一些警告

    class Results(QRunnable):
        def __init__(self):
        super(Results, self).__init__()


        def run(self):
            plt.plot([1,2,3,4],[1,2,3,4])
            plt.savefig('plot.pdf')

    class MainWindow(QMainWindow):


        def __init__(self, *args, **kwargs):
            super(MainWindow, self).__init__(*args, **kwargs)
            self.threadpool = QThreadPool()
            

            layout = QVBoxLayout()
    
            self.l = QLabel("Start")
            b = QPushButton("DANGER!")
            b.pressed.connect(self.oh_no)

            layout.addWidget(self.l)
            layout.addWidget(b)

            w = QWidget()
            w.setLayout(layout)

            self.setCentralWidget(w)

            self.show()

        def oh_no(self):
            worker = Results()
            self.threadpool.start(worker)


    app = QApplication([])
    window = MainWindow()
    app.exec_()
这些警告是:

 QPixmap: It is not safe to use pixmaps outside the GUI thread
 QPainter::begin: Paint device returned engine == 0, type: 2
 QPainter::setRenderHint: Painter must be active to set rendering hints
 QPainter::setBrush: Painter not active
 QPainter::translate: Painter not active
 QPainter::translate: Painter not active
 QPainter::setBrush: Painter not active
 QPainter::setPen: Painter not active
 QPainter::translate: Painter not active
 QPainter::setBrush: Painter not active
 QPainter::setPen: Painter not active
 QPixmap: It is not safe to use pixmaps outside the GUI thread
 QObject: Cannot create children for a parent that is in a different thread.
 (Parent is Oxygen::WidgetStateEngine(0x282cdc0), parent's thread is QThread(0x141baf0), current thread is QThread(0x2a87e60)

请提供@eyllanesc,我已更改代码以添加适当的示例。因为我没有在主线程中绘图,所以它会给出多个警告。如果我刚刚将run()函数命令添加到oh_no函数并删除线程池命令。它工作得很好