<div dir="ltr"><div>Hello everyone,<br><br></div>I'm trying to plot live datas using matplotlib and PyQt. I need a multithreaded program beacause I use time.sleep and it freeze completely the app during that moment. I've tried that but it crash immediatly:<br>
<br><br>from PyQt4 import QtCore, QtGui
<p style="margin:0px;text-indent:0px">import time</p>
<p style="margin:0px;text-indent:0px">import sys</p>
<p style="margin:0px;text-indent:0px">from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas</p>
<p style="margin:0px;text-indent:0px">from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar</p>
<p style="margin:0px;text-indent:0px">from matplotlib.figure import Figure</p>


<p style="margin:0px;text-indent:0px"># Subclassing QThread</p>
<p style="margin:0px;text-indent:0px"># <a href="http://doc.qt.nokia.com/latest/qthread.html">http://doc.qt.nokia.com/latest/qthread.html</a></p>

<p style="margin:0px;text-indent:0px">class Graph(FigureCanvas):</p>
<p style="margin:0px;text-indent:0px">    def __init__(self,parent):</p>

<p style="margin:0px;text-indent:0px">        self.fig = Figure()</p>
<p style="margin:0px;text-indent:0px">        <a href="http://self.ax">self.ax</a> = self.fig.add_subplot(111)   </p>
<p style="margin:0px;text-indent:0px">        FigureCanvas.__init__(self, self.fig)             </p>

<p style="margin:0px;text-indent:0px">        self.R1 = []</p>
<p style="margin:0px;text-indent:0px">        self.l_R1, = self.ax.plot([], self.R1,"-o")     </p>
<p style="margin:0px;text-indent:0px">        self.fig.canvas.draw()</p>
<p style="margin:0px;text-indent:0px">        FigureCanvas.updateGeometry(self) </p>

<p style="margin:0px;text-indent:0px">class ApplicationWindow(QtGui.QMainWindow):</p>
<p style="margin:0px;text-indent:0px">    """Example main window"""</p>
<p style="margin:0px;text-indent:0px">    def __init__(self):</p>
<p style="margin:0px;text-indent:0px">        QtGui.QMainWindow.__init__(self)</p>
<p style="margin:0px;text-indent:0px">        self.main_widget = QtGui.QWidget(self)</p>
<p style="margin:0px;text-indent:0px">        vbl = QtGui.QGridLayout(self.main_widget)      </p>
<p style="margin:0px;text-indent:0px">        qmc = Graph(self.main_widget)</p>
<p style="margin:0px;text-indent:0px">        vbl.addWidget(qmc,0,0,1,11)</p>
<p style="margin:0px;text-indent:0px">        self.setCentralWidget(self.main_widget)</p>


<p style="margin:0px;text-indent:0px">class AThread(QtCore.QThread):</p>

<p style="margin:0px;text-indent:0px">    def run(self):</p>
<p style="margin:0px;text-indent:0px">        count = 0</p>
<p style="margin:0px;text-indent:0px">        while count < 5:</p>
<p style="margin:0px;text-indent:0px">            time.sleep(1)</p>
<p style="margin:0px;text-indent:0px">            print "Increasing"</p>
<p style="margin:0px;text-indent:0px">            count += 1</p>
<p style="margin:0px;text-indent:0px">            aw.l_R1.set_data(count, count)</p>




<p style="margin:0px;text-indent:0px">def usingQThread():</p>
<p style="margin:0px;text-indent:0px">    app = QtCore.QCoreApplication([])</p>
<p style="margin:0px;text-indent:0px">    thread = AThread()</p>
<p style="margin:0px;text-indent:0px">    thread.finished.connect(app.exit)</p>
<p style="margin:0px;text-indent:0px">    thread.start()</p>
<p style="margin:0px;text-indent:0px">    sys.exit(app.exec_())</p>



<p style="margin:0px;text-indent:0px">if __name__ == "__main__":</p>
<p style="margin:0px;text-indent:0px">#    </p>
<p style="margin:0px;text-indent:0px">    </p>
<p style="margin:0px;text-indent:0px">    qApp = QtGui.QApplication(sys.argv)</p>
<p style="margin:0px;text-indent:0px">    aw = ApplicationWindow()</p>
<p style="margin:0px;text-indent:0px">    aw.showMaximized()</p>
<p style="margin:0px;text-indent:0px">    usingQThread()</p>
<p style="margin:0px;text-indent:0px">    sys.exit(qApp.exec_())</p><p style="margin:0px;text-indent:0px"><br></p><p style="margin:0px;text-indent:0px">Could you explain me why? The aim is to use one thread to acquire the data and another to refresh the graph.</p>
<p style="margin:0px;text-indent:0px"><br></p><p style="margin:0px;text-indent:0px">Thanks!</p><p style="margin:0px;text-indent:0px"><br></p><p style="margin:0px;text-indent:0px">Fabien<br></p><br></div>