[PyQt] Multithreading and plotting

Fabien Lafont lafont.fabien at gmail.com
Thu Dec 20 16:32:23 GMT 2012


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20121220/db2521fa/attachment.html>


More information about the PyQt mailing list