python - 在 PyQt4 中使用 PyQtGraph 实时绘图 #2

标签 python pyqt pyqt4 qthread pyqtgraph

首先,对于篇幅感到抱歉。我想尽可能好地解释我的问题。我对 Python 很陌生,并尝试使用 PyQt4 中嵌入的 PyQtGraph 制作一个绘图应用程序。几天前,我的 plotting problem 得到了一个非常好的答案。 ,我的下一步是让两个 PyQtGraphs 绘图小部件同时在同一个 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 类中我用 Plots.print 替换“button.connect”参数,则效果很好。所以我可以看到,我在 MainWindow 中创建一个 LoginWidget 对象,然后 Plots 继承自 MainWindow,再次调用同一个 LoginWidget 对象,这一事实有问题。

我尝试了一个建议的解决方案,其中父亲(MainWindow)不访问 child (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_())

最佳答案

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

计时器版本

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

关于python - 在 PyQt4 中使用 PyQtGraph 实时绘图 #2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41718719/

相关文章:

python - pyqtgraph:在缩放时保持 TextItem 的恒定位置

python - PyQt4 中的 CSS 显示错误

查找未包含在另一个 python 列表中的 python 列表元素的 Pythonic 方法

python - Pandas 汇总统计的经济状况调查有何不同?

python - 澄清行为 : collections. defaultdict 与 dict.setdefault

python - 使用 pyrcc5 时出错

python - cr.execute在OpenERP开发中是什么意思?

python - 我如何使用 QTime() 制作时间计数器

Python PyQt4 无效语法

python - PyQt4 菜单从右到左对齐