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