【问题标题】:pyqtgraph real time updating graph not showingpyqtgraph实时更新图不显示
【发布时间】:2017-07-02 12:27:14
【问题描述】:

我尝试了以下代码,该代码在单个脚本文件中运行,我可以看到实时更新的图表,

from PyQt4 import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
from pyqtgraph.ptime import time

app = QtGui.QApplication([])

pw = pg.plot()
timer = pg.QtCore.QTimer()


def update():
    x = np.random.normal(size=(100))
    y = np.random.normal(size=(100))
    pw.plot(x, y, clear=True)


timer.timeout.connect(update)
timer.start(0)

## Start Qt event loop unless running in interactive mode.
if __name__ == '__main__':
    import sys

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

但是,如果我将实时更新部分放入对话框中单击按钮调用的函数中,则图表不会显示任何内容,

class TestDialog(QtGui.QDialog):
    def __init__(self, parent):
        super(TestDialog, self).__init__(parent, flags=QtCore.Qt.WindowMinimizeButtonHint|QtCore.Qt.WindowMaximizeButtonHint)

    self.resize(1000,618)

    self.chart_button = QtGui.QPushButton('Show chart', self)
    self.chart_button.clicked.connect(self.show_chart)
    vbox = QtGui.QVBoxLayout()
    vbox.addwidget(self.chart_button)
    self.setLayout(vbox)

    def show_chart(self):
        pw = pg.plot()
        timer = pg.QtCore.QTimer()

        def update():
            x = np.random.normal(size=(100))
            y = np.random.normal(size=(100))
            pw.plot(x, y, clear=True)

        timer.timeout.connect(update)
        timer.start(0)
        if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
            QtGui.QApplication.instance().exec_()

如果我在update 函数中设置了一个断点,我发现它没有在第二个脚本中被调用,我可以知道为什么吗?

【问题讨论】:

  • 也许你可以把手放回主窗口?函数 show_cart 不在这里控制更新过程。 self.parent = parent #under super(Test...... self.parent.processEvents() #在更新函数结束时更新应该是self.update ?

标签: python pyqt pyqt4 pyqtgraph


【解决方案1】:

您只需将父级传递给计时器。改变

timer = pg.QtCore.QTimer()

timer = pg.QtCore.QTimer(self)

【讨论】:

    【解决方案2】:

    以下代码适用于我:

    # -*- coding: utf-8 -*-
    from PyQt4 import QtGui, QtCore
    import numpy as np
    import pyqtgraph as pg
    import sys
    from pyqtgraph.ptime import time
    
    
    
    class TestDialog(QtGui.QMainWindow):
        def __init__(self, parent):
            super(TestDialog, self).__init__()
            self.parent=parent
            self.centralWidget = QtGui.QWidget()
            self.setCentralWidget(self.centralWidget)
            self.resize(1000,618)
            self.vbox = QtGui.QVBoxLayout()
    
            self.chart_button = QtGui.QPushButton('Show chart', self)
            self.pw = pg.PlotWidget()
            self.vbox.addWidget(self.chart_button)
            self.vbox.addWidget(self.pw)
            self.centralWidget.setLayout(self.vbox)
            x = np.random.normal(size=(100))
            y = np.random.normal(size=(100))
            self.pw.plot(x, y,clear=True)
            self.chart_button.clicked.connect(self.show_chart)
    
    
        def update(self): 
            x = np.random.normal(size=(100))
            y = np.random.normal(size=(100))
            self.pw.plot(x, y, clear=True)
            QtCore.QCoreApplication.processEvents()
    
    
        def show_chart(self):
            self.timer = pg.QtCore.QTimer()
            self.timer.setSingleShot(False)
            self.timer.timeout.connect(self.update)
            self.timer.start(100)
    
    
    
    
    ## Start Qt event loop unless running in interactive mode.
    def main():
        app = QtGui.QApplication(sys.argv)
        ex = TestDialog(app)
        ex.show()
        sys.exit(app.exec_())
    
    
    if __name__ == '__main__':
        main()
    

    如果需要,您还可以将更新保存在 show_chart 函数中。

    def show_chart(self):
        def update( ):
            x = np.random.normal(size=(100))
            y = np.random.normal(size=(100))
            self.pw.plot(x, y, clear=True)
            QtCore.QCoreApplication.processEvents()
        self.update = update
        self.timer = pg.QtCore.QTimer()
        self.timer.setSingleShot(False)
        self.timer.timeout.connect(self.update)
        self.timer.start(100)
    

    【讨论】:

      猜你喜欢
      • 2020-08-04
      • 2019-03-27
      • 2019-12-10
      • 2021-06-14
      • 2019-02-02
      • 2019-02-13
      • 2018-09-12
      • 2017-04-17
      • 2016-12-07
      相关资源
      最近更新 更多