PyQt5从设备实时绘制线程

2024-09-22 16:34:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在构建软件,以便从设备接收数据,并在GUI上实时绘图。这组数据以列表的形式从设备中获取。我查阅了许多方法,似乎“计时器”很有用。然而,当我试图用它来绘图时,它不起作用并且崩溃了。下面是我从设备接收数据的线程freqsweep_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)

请将我从这个线程错误中解救出来,我真的很感激任何想法! 谢谢大家!


Tags: fromimportselfdatasignalinitdefstart
1条回答
网友
1楼 · 发布于 2024-09-22 16:34:18

好的,经过一次又一次的搜索,this的帖子帮了大忙

对于那些可能遇到类似问题的人,以下是我修订的代码。我没有完全正确地理解pyqtSlot

class MainClass(QDialog,LLAGUI1.Ui_Dialog):

    def __init__(self):
        super().__init__()
        self.setupUi(self)

        self.worker = Worker()
        self.make_connection(self.worker)
        self.worker.start()
        self.graph = pg.PlotWidget()
        self.graph.setYRange(-120,10)
        self.plot = self.graph.plot()
        self.gridLayout_graph.addWidget(self.graph, 0, 0)
        self.show()

        self.pushButton_amp_set.clicked.connect(self.setAmp)


    def make_connection(self, data_object):
        data_object.signal.connect(self.grab_data)

    @pyqtSlot(object, object)
    def grab_data(self, data, data1):
        print(data)
        print(data1)
        self.plot.setData(data, data1)

    #
    # def onStart(self):
    #     self.curve = MyWidget()
    #     # self.verticalLayout_3.
    #     self.verticalLayout_3.addWidget(self.curve)

    def setAmp(self):
        Min = float(self.lineEdit_amp_min.text())
        Max = float(self.lineEdit_amp_max.text())
        self.graph.setYRange(Min, Max)

    def CenterSpan(self):
        center = int(self.lineEdit_freq_center.text())
        span = int(self.lineEdit_freq_span.text())

        sa_config_center_span(handle, center, span)
        # self.w = MyWidget()






if __name__=='__main__':
    app=QApplication(sys.argv)

    lla=MainClass()
    lla.show()

    app.exec_()

工作线程如下所示:

class Worker(QThread):
    signal = pyqtSignal(object, object)

    def __init__(self):
        super().__init__()

    def run(self):
        while True:
            freqs = [start_freq + i * bin_size for i in range(sweep_length)]
            sweep_max = list(sa_get_sweep_32f(handle)["max"])
            self.data_x = freqs
            self.data_y = sweep_max
            self.signal.emit(self.data_x, self.data_y)
            time.sleep(0.01)

相关问题 更多 >