我目前正在构建软件,以便从设备接收数据,并在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,我的以下部分代码出现错误。如果没有以下四条线,曲线不是实时的,它只是绘制和停止,即使它一直在接收数据。我试图把make_connection
放在那里,但它也给了我错误
self.timer = QtCore.QTimer(self)
self.timer.setInterval(100) # in milliseconds
self.timer.start()
self.timer.timeout.connect(self.plotCurve)
请将我从这个线程错误中解救出来,我真的很感激任何想法! 谢谢大家!
好的,经过一次又一次的搜索,this的帖子帮了大忙
对于那些可能遇到类似问题的人,以下是我修订的代码。我没有完全正确地理解pyqtSlot
工作线程如下所示:
相关问题 更多 >
编程相关推荐