gpt4 book ai didi

python - pyqtgraph实时更新图不显示

转载 作者:太空宇宙 更新时间:2023-11-04 05:14:02 24 4
gpt4 key购买 nike

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

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 函数中设置了一个断点,我发现它没有在第二个脚本中被调用,我可以知道为什么吗?

最佳答案

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

timer = pg.QtCore.QTimer()

timer = pg.QtCore.QTimer(self)

关于python - pyqtgraph实时更新图不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42222358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com