python - 在线程内更新 pyqtgraph BarGraphItem
问题描述
在绘制实时数据时,我很难让 gui 做出响应。为了使 GUI 不会冻结,我尝试线程化我的所有活动。我想实现以下几点:
- 通过串口记录数据
- 线程中稍后绘图的计算
- 在线程中绘图(当前通过 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)
在我的 Calculator-Thread 中,数据将被计算并交给连接到 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)
这是将我的计算数据从我的计算器线程传递到 BarGraphItem 中使用的双端队列的正确方法吗?如何在线程内更新我的 BarGraphItem?
解决方案
你编程的方式看起来不错。“卡顿”的根本原因似乎是在拖动过程中出现了“更新块”。
尝试通过将 pg.QtGui.QApplication.processEvents() 添加到您的 updatePlot 函数来强制更新,如下所述:
def updatePlot(self):
print("update")
self.barItem.setOpts()
pg.QtGui.QApplication.processEvents()
推荐阅读
- amazon-web-services - 如何从另一个 AWS 账户访问公共 S3 存储桶?
- c# - 如何组合 Swashbuckle 过滤器?
- swift - 如何解决 UIImageView 连接到 Swift4?
- amazon-web-services - Route53 Healthcheck 使用 Terraform 与 Cloudwatch 警报集成
- sql-server - 根据条件在 SQL Server 中运行总计
- c++ - UIAutomation:AddAutomationEventHandler() 返回 E_INVALIDARG
- angular - 如何使用 For 循环 Angular6 动态创建引导模式
- php - 为什么我在在线上传大于 1MB 的文件时使用 Laravel5.4 得到 MethodNotAllowedHttpException,但它在 localhost 上完美运行?
- codeigniter - 在 Codeigniter 3.1.8 中使用购物车类是否安全
- android - 在 android build 中配置 lint 基线文件时出现 Lint 基础结构错误