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

时间:2017-01-18 11:51:33

标签: python pyqt pyqt4 qthread pyqtgraph

首先抱歉这个长度。我想尽可能好地解释我的问题。我是Python的新手,并尝试使用PyQt4中嵌入的PyQtGraph创建一个绘图应用程序。几天前,我得到了一个非常好的答案plotting problem,我的下一步是让两个PyQtGraphs绘制Widgets同时在同一个PyQt4的CentralWidget中绘图。通过与所描述的链接相同的方法,两个图都可以正常工作,但GUI没有响应。为了克服这个问题,我的目标是使用QThread,为此,我需要将我的绘图函数放在不同的类中。但是我搞乱了我的变量,无法看到:

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(Plots.plotter)
        self.central_widget.addWidget(self.login_widget)


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)


class Plots(MainWindow):

    def print(self):
        print('hello World')

    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)

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

这在Plots.plotter上给了我一个错误:

self.data = [0]
AttributeError: 'bool' object has no attribute 'data'

如果在MainWindow课程中我替换了' button.connect'与Plots.print的争论,它工作正常。所以我可以看到我在MainWindow中创建了一个LoginWidget对象,然后Plots从MainWindow继承,再次调用同一个LoginWidget对象。

我尝试了一个建议的解决方案,其中父(MainWindow)不访问子节点(Plots)的方法。但是,如果,从Plots,我想调用我的目标放置我的线程的类,我再次得到相同的错误....

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


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)


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.central_widget.addWidget(self.login_widget)


class Plots(MainWindow):
    def __init__(self, parent=None):
        super(Plots, self).__init__(parent=parent)
        self.login_widget.button.clicked.connect(MyThread.plotter)



class MyThread(MainWindow):
    def __init__(self, parent=None):
        super(Aname, self).__init__(parent=parent)

    def print(self):
        print('hello World')

    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)


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    w = Plots()
    w.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:2)

在继承中,父亲不应该访问子节点的方法,最好继承和实现与子类中父节点无关的新方法。

计时器版本

import random
import sys

import pyqtgraph as pg
from PyQt4 import QtGui, QtCore


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)
        self.button.clicked.connect(self.plotter)

    def plotter(self):
        self.data = [0]
        self.curve = self.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 MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.centralwidget = QtGui.QWidget(self)
        self.setCentralWidget(self.centralwidget)
        self.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget)
        self.login_widget_1 = LoginWidget(self)
        self.horizontalLayout.addWidget(self.login_widget_1)

        self.login_widget_2 = LoginWidget(self)
        self.horizontalLayout.addWidget(self.login_widget_2)

        self.setCentralWidget(self.centralwidget)


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())