为什么这个简单的 pyqtgraph 示例不起作用?

Why this simple example of pyqtgraph is not working?

以下代码只是为了测试pyqtgraph的速度。我期望的是永远得到交替图。但是,执行此代码后,小部件中没有显示任何内容。有什么问题?

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from random import randint, uniform
from math import *
import pyqtgraph as pg
import time

class Example(QWidget):

    def __init__(self):
        super().__init__()
        self.x=pg.PlotWidget(self)
        self.x.setMinimumHeight(400)
        self.x.setMinimumWidth(400)
        self.setWindowState(Qt.WindowMaximized)
        self.u=[i+uniform(1,30) for i in range(1000)]
        self.v=[-i+uniform(1,30) for i in range(1000)]
        self.show()

    def Run(self):
        while 1:
            self.x.clear()
            self.x.plot(self.u)
            self.x.clear()
            self.x.plot(self.v)

app=QApplication(sys.argv)
ex=Example()
ex.Run()
sys.exit(app.exec_())

在 GUI 中使用 while 循环通常不是一个好主意。问题在于它阻止 GUI 保持响应并处理所有 GUI 事件。

一个选项是改用计时器,例如一个简单的QTimer。为了在两个不同的数据集之间切换以进行绘图,您还需要引入一些机制来确定应该显示哪个数据集。

import sys
#from PyQt5.QtWidgets import *
#from PyQt5.QtCore import *
from PyQt4 import QtGui, QtCore
from random import randint, uniform
import pyqtgraph as pg

class Example(QtGui.QWidget):

    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.x=pg.PlotWidget(self)
        self.x.setMinimumHeight(400)
        self.x.setMinimumWidth(400)
        self.setWindowState(QtCore.Qt.WindowMaximized)
        self.u=[i+uniform(1,30) for i in range(1000)]
        self.v=[-i+uniform(1,30) for i in range(1000)]
        self.switch = True
        self.show()

    def start(self):
        self.timer = QtCore.QTimer(self)
        self.timer.timeout.connect(self.run)
        self.timer.start(500)

    def run(self):
        if self.switch:
            self.x.clear()
            self.x.plot(self.u)
        else:
            self.x.clear()
            self.x.plot(self.v)
        self.switch = not self.switch

app=QtGui.QApplication(sys.argv)
ex=Example()
ex.start()
sys.exit(app.exec_())