How to update a realtime plot and use buttons to interact in pyqtgraph?

你离开我真会死。 提交于 2019-12-04 20:16:32
luddek

So this is not really a pyqtgraph question. Mostly you need to read up on classes in python and on pyqt basics. But I will try to give you some quick fixes for your problems, and hopefully you will learn something on the way.

For making your buttons work you need to connect them to the methods. Try looking at the answer here: https://stackoverflow.com/a/8763339/4328381

You would need something like this

def qt_connections(self):
    self.increasebutton.clicked.connect(self.on_increasebutton_clicked)
    self.decreasebutton.clicked.connect(self.on_decreasebutton_clicked)

Then to make the buttons actually do something they need to change your amplitude. First store the amplitude as an attribute for the instance. Also store a t attribute to later make it move.

Your a and ptr1 are just variables inside the method that will be cleared up once the method finishes. By using self. they become instance attributes that can be used from the other methods in the class.

def __init__(self):
    ...
    self.amplitude = 10
    self.t = 0
    ...

Then you can change the amplitude in the method connected to the button.

def on_increasebutton_clicked(self):
    print ("Amplitude increased")
    self.amplitude += 1
    self.updateplot()

Then to make it continous you first need to make sure that the timer works. Try adding a print("test") in it and you will see that it doesn't.You need to keep a reference of it, otherwise it will be cleaned up.

    self.timer = pg.QtCore.QTimer()
    self.timer.timeout.connect(self.moveplot)
    self.timer.start(50)

To make the sine move continuously you need to move it in the method connected to the timer, you can simple create a moveplot method.

def moveplot(self):
    self.t+=1
    self.updateplot()

And then to put it together create and updateplot method which uses the attributes created earlier.

def updateplot(self):
    print "Update"
    data1 = self.amplitude*np.sin(np.linspace(0,30,121)+self.t)
    self.plotcurve.setData(data1)

In the end you will get something like this

import sys
from PyQt4 import QtGui, QtCore
import numpy as np
import pyqtgraph as pg


class sinus_wave(QtGui.QWidget):
    def __init__(self):
        super(sinus_wave, self).__init__()
        self.init_ui()
        self.qt_connections()
        self.plotcurve = pg.PlotCurveItem()
        self.plotwidget.addItem(self.plotcurve)
        self.amplitude = 10
        self.t = 0
        self.updateplot()

        self.timer = pg.QtCore.QTimer()
        self.timer.timeout.connect(self.moveplot)
        self.timer.start(500)

    def init_ui(self):
        self.setWindowTitle('Sinuswave')
        hbox = QtGui.QVBoxLayout()
        self.setLayout(hbox)

        self.plotwidget = pg.PlotWidget()
        hbox.addWidget(self.plotwidget)

        self.increasebutton = QtGui.QPushButton("Increase Amplitude")
        self.decreasebutton = QtGui.QPushButton("Decrease Amplitude")

        hbox.addWidget(self.increasebutton)
        hbox.addWidget(self.decreasebutton)

        self.setGeometry(10, 10, 1000, 600)
        self.show()

    def qt_connections(self):
        self.increasebutton.clicked.connect(self.on_increasebutton_clicked)
        self.decreasebutton.clicked.connect(self.on_decreasebutton_clicked)

    def moveplot(self):
        self.t+=1
        self.updateplot()

    def updateplot(self):
        print "Update"
        data1 = self.amplitude*np.sin(np.linspace(0,30,121)+self.t)
        self.plotcurve.setData(data1)

    def on_increasebutton_clicked(self):
        print ("Amplitude increased")
        self.amplitude += 1
        self.updateplot()

    def on_decreasebutton_clicked(self):
        print ("Amplitude decreased")
        self.amplitude -= 1
        self.updateplot()

def main():
    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('Sinuswave')
    ex = sinus_wave()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!