Python 在类实例调用时崩溃

标签 python class crash custom-component pyqtgraph

我正在使用 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/

相关文章:

python - 如何在 Python 中删除输出文件中的重复条目?

Java访问类变量?

python - 类变量作为封闭类的实例

ios - 子类 UIView 有两个标签

ios - 是否可以在不使用崩溃报告工具、AppStore 等的情况下在 iOS 设备本身中获取崩溃日志

python - 使用Vim Retab解决TabError : inconsistent use of tabs and spaces in indentation?

Python在类中初始化一个列表

python - 字符到python中的键码

xcode - 什么是 LLDB RPC 服务器?它什么时候在 Xcode 中崩溃?为什么会崩溃?

android - Android应用在startActivity崩溃( Intent )