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

常春藤

我对使用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()的布局。为此,我在MainWindow中设置了一个CentralWidget。我创建了一个按钮,该按钮应该像前面的代码一样开始绘制,但是当我通过plotter函数调用updater函数时,什么也没有发生:

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_()

因为我必须穿线,所以什么也没发生?

认真的重要性

代码中有几件事情要考虑。
首先,朝着与@luddek答案相同的方向,您需要跟踪变量。如果您在类方法内定义变量,并且该类方法执行完毕,则该变量将丢失。从外面也看不到它。
因此这是一个好主意,用实例变量,
self.plot而不是plot
self.timer代替timer
self.login_widget,而不是login_widget
(也,我不会使用电子书籍global在PyQt的PROGRAMM,虽然它是有效的代码)

接下来,PlotWidget没有setData方法。将数据绘制到PlotItem的图上会涉及更多点:
pg.PlotWidget()具有PlotItem,您可以通过获取.getPlotItem()然后,您需要对其进行调用plot(),这将返回您要向其添加数据的实际曲线。在下面的示例中,我介绍了新变量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_()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章