在 PyQt4 中使用 PyQtGraph 进行实时绘图

Live Plotting with PyQtGraph in PyQt4

我是 Python 的新手,正在尝试制作一个 PyQt4 应用程序,我在其中嵌入了 PyQtGraph。我有这个 PyQtGraph 实时绘图仪,效果非常好:

from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import random

app = QtGui.QApplication([])
p = pg.plot()
curve = p.plot()
data = [0]

def updater():

    data.append(random.random())
    curve.setData(data) #xdata is not necessary


timer = QtCore.QTimer()
timer.timeout.connect(updater)
timer.start(0)

if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

为了嵌入更大的 PyQt4 应用程序,我需要一个包含 pyqtgraph.PlotWidget() 的布局。为此,我在主窗口中设置了一个 centralWidget。我创建了一个按钮,它应该像前面的代码一样开始绘图,但是当我通过绘图仪函数调用更新函数时什么也没有发生:

import sys
from PyQt4 import QtCore, QtGui
import pyqtgraph as pg
import random

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.central_widget = QtGui.QStackedWidget()
        self.setCentralWidget(self.central_widget)
        login_widget = LoginWidget(self)#to say where the button is
        login_widget.button.clicked.connect(self.plotter)
        self.central_widget.addWidget(login_widget)

    def plotter(self):
        self.data =[0]
        timer = QtCore.QTimer()
        timer.timeout.connect(self.updater)
        timer.start(0)

    def updater(self):

        self.data.append(random.random())
        plot.setData(self.data)

class LoginWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        global plot
        super(LoginWidget, self).__init__(parent)
        layout = QtGui.QHBoxLayout()
        self.button = QtGui.QPushButton('Start Plotting')
        layout.addWidget(self.button)
        plot = pg.PlotWidget()
        layout.addWidget(plot)
        self.setLayout(layout)

if __name__ == '__main__':
    app = QtGui.QApplication([])
    window = MainWindow()
    window.show()
    app.exec_()

没有任何事情发生是因为我必须穿线吗?

updater() 未被调用,因为计时器已被垃圾收集器删除。

您需要在某处保留对计时器的引用。例如,您可以在 __init__

中创建引用
def __init__(self, parent=None):
    ...
    self.timer = QtCore.QTimer()

代码中有几点需要考虑。
首先,按照与@luddek 的回答相同的方向,您需要跟踪您的变量。如果您在 class 方法中定义了一个变量,并且此 class 方法执行完毕,则该变量将丢失。而且从外面是看不到的。
因此,使用实例变量是个好主意,
self.plot 而不是 plot
self.timer 而不是 timer
self.login_widget 而不是 login_widget
(另外,我不建议在 PyQt 程序中使用 global,尽管它是有效代码)

接下来,PlotWidget 没有 setData 方法。将数据绘制到 PlotItem 的绘图中有点复杂:
pg.PlotWidget() 有一个 PlotItem,您可以通过 .getPlotItem() 获得它。然后您需要对其调用 plot(),returns 您要向其添加数据的实际曲线。在下面的示例中,我引入了新变量 self.curve,您可以通过 self.curve.setData(self.data)

添加数据

这是完整的工作代码。

from PyQt4 import QtCore, QtGui
import pyqtgraph as pg
import random

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.central_widget = QtGui.QStackedWidget()
        self.setCentralWidget(self.central_widget)
        self.login_widget = LoginWidget(self)
        self.login_widget.button.clicked.connect(self.plotter)
        self.central_widget.addWidget(self.login_widget)

    def plotter(self):
        self.data =[0]
        self.curve = self.login_widget.plot.getPlotItem().plot()

        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.updater)
        self.timer.start(0)

    def updater(self):

        self.data.append(self.data[-1]+0.2*(0.5-random.random()) )
        self.curve.setData(self.data)

class LoginWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(LoginWidget, self).__init__(parent)
        layout = QtGui.QHBoxLayout()
        self.button = QtGui.QPushButton('Start Plotting')
        layout.addWidget(self.button)
        self.plot = pg.PlotWidget()
        layout.addWidget(self.plot)
        self.setLayout(layout)

if __name__ == '__main__':
    app = QtGui.QApplication([])
    window = MainWindow()
    window.show()
    app.exec_()