的Python:PyQt的,的QThread和matplotlib:次要情节崩溃的第二个呼叫

问题描述 投票:1回答:1

我试图使用matplotlib线程的结果绘制图表。首先,我使用PyQt的按钮来启动线程,一切正常。但我第二次按下按钮,次要情节崩溃,因为我想的元组不能被修改。这是我的simplfied代码,你可以自己试试看:

from PyQt4 import QtGui, QtCore
import matplotlib.pyplot as plt
import numpy as np
from PyQt4.Qt import *
import sys

class thread(QThread):
  def __init__(self, parent=None):
      QThread.__init__(self, parent)

  def __del__(self):
      self.wait()

  def render(self):
      self.start()

  def run(self):
      data = [(1, 10), (3, 100), (4, 1000), (5, 100000)]
      data_in_array = np.array(data)
      transposed = data_in_array.T
      x, y = transposed
      fig, ax = plt.subplots(1,1)
      ax.plot(x, y, 'ro')
      ax.plot(x, y, 'b-')
      plt.show()

class Window(QtGui.QWidget):
   def __init__(self):
        QtGui.QWidget.__init__(self)
        self.button = QtGui.QPushButton('Test', self)
        self.button.clicked.connect(self.handleButton)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.button)
        self.thread=thread()

   def handleButton(self):
        self.thread.render()

if __name__ == '__main__':
  app = QtGui.QApplication(sys.argv)
  window = Window()
  window.show()
  sys.exit(app.exec_())

拜托,我怎么能解决这个问题,以避免崩溃,并使用好几次我的按钮?谢谢

python multithreading matplotlib pyqt4
1个回答
0
投票

这不是混合您Qt应用程序和Matplotlib(其中使用Qt在其后端)的正确途径。

https://matplotlib.org/gallery/user_interfaces/embedding_in_qt_sgskip.html一个更好的解释比我能提供。

© www.soinside.com 2019 - 2024. All rights reserved.