gpt4 book ai didi

Python 在类实例调用时崩溃

转载 作者:行者123 更新时间:2023-12-02 17:25:18 24 4
gpt4 key购买 nike

我正在使用 pyqtgraph 进行实时数据绘图。不幸的是,该库不支持时间序列实时绘图,所以我去谷歌搜索,发现有人为此编写了特定的类(你可以找到它 here )。我曾经使用过这些类,没有任何问题,但现在 python 在调用“TimeSeriesPlot()”类时立即崩溃。我尝试删除 python 文件夹和所有子文件夹中的所有 .pyc 文件,但这没有改变任何内容,也没有修复 python 安装。关于导致问题的原因以及如何解决有什么想法吗?有人遇到过这样的问题吗?我的配置是:

  • Windows 7 家庭版 block 引用
  • Python 2.7.5
  • Pyside 1.1.2 和 QT 4.8
  • PyQtGraph 0.9.7

这是我一直尝试执行的文件的内容:

from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg

class TimeSeriesPlotViewBox(pg.ViewBox):
def __init__(self, timeSeriesPlot, *args, **kwds):
pg.ViewBox.__init__(self, *args, **kwds)
self.timeSeriesPlot = timeSeriesPlot


def mouseClickEvent(self, ev):
if ev.button() == QtCore.Qt.RightButton:

self.timeSeriesPlot.xFrom = None
self.timeSeriesPlot.xTo = None
self.timeSeriesPlot.updateView()

self.enableAutoRange(pg.ViewBox.XAxis,1.0)
self.enableAutoRange(pg.ViewBox.YAxis,1.0)


def mouseDoubleClickEvent(self, ev):
if ev.button() == QtCore.Qt.LeftButton:

xFrom = 0
if not self.timeSeriesPlot.xFrom is None and self.timeSeriesPlot.xFrom < 0:
xFrom = self.timeSeriesPlot.xFrom
else:
xFrom = -len(self.timeSeriesPlot.timedata)
self.timeSeriesPlot.xFrom = int(round(xFrom / 2.0))


self.timeSeriesPlot.updateView()
self.enableAutoRange(pg.ViewBox.XAxis,1.0)
self.enableAutoRange(pg.ViewBox.YAxis,1.0)

def mouseDragEvent(self, ev):
if ev.button() == QtCore.Qt.RightButton:
ev.ignore()
else:
pg.ViewBox.mouseDragEvent(self, ev)


class TimeSeriesPlot(pg.QtCore.QObject):

def __init__(self, tsTitle, parent = None):
pg.QtCore.QObject.__init__(self, parent)

self.vb =TimeSeriesPlotViewBox(self)

self.plt = pg.PlotWidget(viewBox=self.vb, title = tsTitle)
self.vb.sigRangeChangedManually.connect(self.zoom)

#time axis
self.timedata = []

#val data
self.valdata = []
self.curveVal = pg.PlotDataItem([])
self.plt.addItem(self.curveVal)

self.xFrom = -50
self.xTo = None

def zoom(self):
xlimits,ylimits = self.vb.viewRange()

import bisect
self.xFrom = bisect.bisect_left(self.timedata,xlimits[0])
self.xTo = bisect.bisect_right(self.timedata,xlimits[1])

self.vb.disableAutoRange(pg.ViewBox.XAxis)
self.vb.disableAutoRange(pg.ViewBox.YAxis)

self.updateView()

def show(self):
self.plt.show()

def updateModel(self,newdata):
time = float(newdata["time"])
val = float(newdata["val"])

self.timedata.append(time)
self.valdata.append(val)

def updateView(self):
useAA = True
viewSlice = None
maxElementCnt = 500.0
if self.xFrom is None and self.xTo is None:
elementCnt = len(self.timedata)
step = max(int(round(elementCnt / maxElementCnt)),1)
viewSlice = slice(-elementCnt,None,step)
elif self.xFrom < 0:
elementCnt = -self.xFrom
step = max(int(round(elementCnt / maxElementCnt)),1)
viewSlice = slice(-elementCnt,None,step)
else:
elementCnt = self.xTo - self.xFrom
step = max(int(round(elementCnt / maxElementCnt)),1)
viewSlice = slice(self.xFrom,self.xTo,step)

useAA = True
self.curveVal.setData(x=self.timedata[viewSlice],y=self.valdata[viewSlice],clear=True,antialias=useAA)
####################################
TimeSeriesPlot('plot')

最佳答案

运行此代码时看到的第一条错误消息是:

QWidget: Must construct a QApplication before a QPaintDevice

对于所有 Qt 应用程序都是如此;您必须先创建一个 QApplication 实例。修复此问题后,脚本仍然因段错误而崩溃,可能是因为 TimeSeriesPlot 实例被立即删除(因为它没有分配给任何变量)。即使修复后,脚本也会立即退出(因为没有其他事情可做)。我用以下代码替换了示例中的最后一行以获得预期结果:

app = pg.QtGui.QApplication([])
tsp = TimeSeriesPlot('plot')
tsp.show()
app.exec_()

关于Python 在类实例调用时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16926044/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com