0
我曾尝试下面的代码,在一个脚本文件的工作原理图,我可以看到一个图表被实时更新,pyqtgraph实时更新不显示
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(测试...... self.parent.processEvents()#在更新函数末尾 update应该是self.update? – ymmx