python - 更新线程内的pyqtgraph BarGraphItem

标签 python multithreading pyqt pyqtgraph

在绘制实时数据时,我很难使gui响应。为了使GUI不会冻结,我尝试对所有 Activity 进行线程化。我想了解以下内容:

  • 通过串行端口
  • 记录数据
  • 线程
  • 中后面图的计算
  • 在线程中进行绘图(当前通过QTimer进行,但是当我拖动窗口时,始终会出现“小”冻结,并且在拖动时绘图不会更新)

  • 1和2已完成,但是现在我不确定如何在单独的线程中更新绘图。
    我的PlotWidget启动看起来像这样:
    self.plottingDQ = [queue.deque(maxlen=100), queue.deque(maxlen=100), queue.deque(maxlen=100)]
    self.graph = pg.PlotWidget()
    self.barItem = pg.BarGraphItem(x0=self.plottingDQ[0], y0=self.plottingDQ[1], width=self.plottingDQ[2], height=1)
    self.graph.addItem(self.barItem)
    
    通过连接到该功能的按钮完成启动线程。 Writer-Thread是无关紧要的,因为它与绘图无关。但是计算器线程会计算数据以更新绘图
    def startPlotting(self):
        # not relevant for the example
        self.csvData = queue.Queue() 
        self.csv = Writer(self.csvData)
        self.csv.setDaemon(True)
        self.csv.start()
    
        self.calcData = queue.Queue()
        self.calcPlot = Calculator(self.calcData, self.plottingDQ)
        self.calcPlot.setDaemon(True)
        self.calcPlot.start()
    
        # Timer to update plot every x ms
        self.timer = QTimer()
        self.timer.timeout.connect(self.updatePlot)
        self.timer.start(500)
    
    现在我每500毫秒更新一次Qtimer中的绘图
    def updatePlot(self):
        print("update")
        self.barItem.setOpts()
    
    因此,每当我从串行端口获得一些输入时,我都将数据传递给线程并调用如下代码:
    def fromPort(self, data):
        self.csvData.put(data)
        self.calcData.put(data)
    
    在我的计算器线程中,将计算数据并将其移交给与BarGraphItem连接的plottingDQ
    class Calculator(threading.Thread):
        def __init__(self, calcData, plottingDQ):
            threading.Thread.__init__(self)
            self.calcData = calcData
            self.plottingDQ = plottingDQ
            self.a = threading.Event()
            self.a.set()
    
        def run(self):
            while self.a.isSet():
                # Do some stuff here ...
                # After the calculations, i write the data into the plottingDQ
                
                self.plottingDQ[0].append(x)
                self.plottingDQ[1].append(y)
                self.plottingDQ[2].append(duration)
    
    这是将我的计算数据从Calcualtor-Thread传递到BarGraphItem中使用的双端队列的正确方法吗?如何在线程中更新BarGraphItem?

    最佳答案

    您编程的方式看起来不错。
    “拖停”的根本原因似乎是拖动过程中的“更新块”。
    尝试通过将pg.QtGui.QApplication.processEvents()添加到updatePlot函数(described here)来强制执行更新:

    def updatePlot(self):
        print("update")
        self.barItem.setOpts()
        pg.QtGui.QApplication.processEvents()
    

    关于python - 更新线程内的pyqtgraph BarGraphItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65183612/

    相关文章:

    python - 如何在大型 python 代码库中用一个制表符替换四个空格?

    android - Facebook 和 GoogleAnalytics 导致致命信号 11 (SIGSEGV)

    java套接字和线程问题

    python - 我想在 TreeView 中使用 QFileSystemModel 吗?

    python - QLineEdit 中的 title() 问题?

    python - 使用python复制netcdf文件

    Python 线程处理 - join() - 多线程

    python - 在 Python 中将字符串(使用科学记数法)转换为 int

    java - 为什么总是在循环中调用 wait()

    python - PyQt5 - 为什么我的小部件不更新,除非我切换到不同的窗口?