【发布时间】:2021-03-19 07:00:43
【问题描述】:
我目前正在构建软件以从设备接收数据并在我的 GUI 上实时绘图。这组数据来自设备的列表。我查了很多方法,似乎“计时器”很有用。但是,当我尝试使用它进行绘图时,它不起作用并崩溃。以下是我从设备接收数据的线程。 freq 和 sweep_max 是从设备 API 文件中定义的。
class Worker(QThread):
signal_x = pyqtSignal(object, object)
def __init__(self):
super().__init__()
def run(self):
while True:
self.data_x = freqs
self.data_y = sweep_max
self.signal_x.emit(self.data_x, self.data_y)
time.sleep(0.1)
以下是我的带有 GUI 的主要 python 文件。
import sys
from PyQt5.QtCore import *
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtWidgets import *
import LLAGUI1
from SignalThread import MyWidget, Worker
from sadevice.sa_api import *
from PyQt5 import QtCore, QtWidgets
import pyqtgraph as pg
class MainClass(QDialog,LLAGUI1.Ui_Dialog):
def __init__(self):
super().__init__()
self.setupUi(self)
############Part of real time plotting but getting error because of plotCurve has no input
self.timer = QtCore.QTimer(self)
self.timer.setInterval(100) # in milliseconds
self.timer.start()
self.timer.timeout.connect(self.plotCurve)
#############################################################################
self.mywid = pg.GraphicsWindow()
self.plotItem = self.mywid.addPlot(title="Linewidth Measurement")
self.plotDataItem = self.plotItem.plot([], symbolSize=1, symbolPen=None)
self.graphWidget.addItem(self.plotDataItem)
self.make_connection()
def make_connection(self):
self.w = Worker()
self.w.start()
self.w.signal_x.connect(self.plotCurve)
def plotCurve(self, x, y):
self.plotDataItem.setData(x,y)
print(x)
print(y)
if __name__=='__main__':
app=QApplication(sys.argv)
lla=MainClass()
lla.show()
app.exec_()
由于没有输入 plotCurve,我的以下部分代码出现错误。如果不遵循 4 行,曲线就不是实时的,即使它一直在接收数据,它也只是绘制并停止。我试图将make_connection 放在那里,但它也给了我错误。
self.timer = QtCore.QTimer(self)
self.timer.setInterval(100) # in milliseconds
self.timer.start()
self.timer.timeout.connect(self.plotCurve)
请把我从这个线程错误中解救出来,我真的很感激任何想法! 谢谢!
【问题讨论】:
-
您正在访问未定义的属性。你在哪里设置属性
MainClass.plotCurve? -
@bnaecker 我不确定我是否正确理解了您的问题。你的意思是我没有定义
plotCurve吗?如果是这样,如果向下滚动,它会在MainClass中定义。如果你的意思是plotCurve的输入,它没有设置,我不知道在哪里设置它,因为它应该是实时输入。make_connection正在接收线程数据并传递给plotCurve。 -
啊,我错过了,忽略我:)
标签: python multithreading plot pyqt5