【发布时间】:2017-06-04 10:26:11
【问题描述】:
我正在尝试制作一个应用程序,我希望有几个 PlotWidgets 可以绘制来自我的 Arduino 中多达 5 个传感器的信号。一旦我有两个更新的绘图,GUI 就没有响应,我需要暂停/重新启动绘图,并弹出一些值的警报。为了解决这个问题,我已经开始研究以使用 QThread,但这对于 PyQtGraph 可能是不可能的,因为我们不能在多个线程中完成绘图?我的两个 PlotWidgets 代码如下所示:
from PyQt4 import QtCore, QtGui
import pyqtgraph as pg
import random
import sys
class MainWindow(QtGui.QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
layout = QtGui.QHBoxLayout()
self.button = QtGui.QPushButton('Start Plotting Left')
layout.addWidget(self.button)
self.button.clicked.connect(self.plotter)
self.button2 = QtGui.QPushButton('Start Plotting Right')
layout.addWidget(self.button2)
self.button2.clicked.connect(self.plotter2)
self.plot = pg.PlotWidget()
layout.addWidget(self.plot)
self.plot2 = pg.PlotWidget()
layout.addWidget(self.plot2)
self.setLayout(layout)
def plotter(self):
self.data =[0]
self.curve = self.plot.getPlotItem().plot()
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.updater)
self.timer.start(0)
def updater(self):
self.data.append(self.data[-1]+0.2*(0.5-random.random()) )
self.curve.setData(self.data)#Downsampling does not help
def plotter2(self):
self.data2 =[0]
self.curve2 = self.plot2.getPlotItem().plot()
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.updater2)
self.timer.start(0)
def updater2(self):
self.data2.append(self.data[-1]+0.2*(0.5-random.random()) )
self.curve2.setData(self.data) #Downsampling does not help
if __name__ == '__main__':
app = QtGui.QApplication([])
window = MainWindow()
window.show()
app.exec_()
我已经准备好从 QThread 阅读和尝试很多东西,但首先我需要知道这是否可能,或者我是否在浪费时间和睡眠。有没有人暗示我怎样才能让它工作?
【问题讨论】:
标签: python multithreading pyqt qthread pyqtgraph