[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    [PyQt]  Multithreading and plotting
From:       Fabien Lafont <lafont.fabien () gmail ! com>
Date:       2012-12-20 16:32:23
Message-ID: CAC9H_chunLCx2Mju8oAH=PpH2a5G7Q7v_UPAYZPQPfgENGU+Ug () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Hello everyone,

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:


from PyQt4 import QtCore, QtGui

import time

import sys

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as
FigureCanvas

from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as
NavigationToolbar

from matplotlib.figure import Figure

# Subclassing QThread

# http://doc.qt.nokia.com/latest/qthread.html

class Graph(FigureCanvas):

def __init__(self,parent):

self.fig = Figure()

self.ax = self.fig.add_subplot(111)

FigureCanvas.__init__(self, self.fig)

self.R1 = []

self.l_R1, = self.ax.plot([], self.R1,"-o")

self.fig.canvas.draw()

FigureCanvas.updateGeometry(self)

class ApplicationWindow(QtGui.QMainWindow):

"""Example main window"""

def __init__(self):

QtGui.QMainWindow.__init__(self)

self.main_widget = QtGui.QWidget(self)

vbl = QtGui.QGridLayout(self.main_widget)

qmc = Graph(self.main_widget)

vbl.addWidget(qmc,0,0,1,11)

self.setCentralWidget(self.main_widget)

class AThread(QtCore.QThread):

def run(self):

count = 0

while count < 5:

time.sleep(1)

print "Increasing"

count += 1

aw.l_R1.set_data(count, count)

def usingQThread():

app = QtCore.QCoreApplication([])

thread = AThread()

thread.finished.connect(app.exit)

thread.start()

sys.exit(app.exec_())

if __name__ == "__main__":

#

 qApp = QtGui.QApplication(sys.argv)

aw = ApplicationWindow()

aw.showMaximized()

usingQThread()

sys.exit(qApp.exec_())


Could you explain me why? The aim is to use one thread to acquire the data
and another to refresh the graph.


Thanks!


Fabien

[Attachment #5 (text/html)]

<div dir="ltr"><div>Hello everyone,<br><br></div>I&#39;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&#39;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,&quot;-o&quot;)     </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">    &quot;&quot;&quot;Example main \
window&quot;&quot;&quot;</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 &lt; 5:</p>
<p style="margin:0px;text-indent:0px">            time.sleep(1)</p>
<p style="margin:0px;text-indent:0px">            print &quot;Increasing&quot;</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__ == &quot;__main__&quot;:</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>



_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic