程序执行时 pyqtgraph 图不显示

pyqtgraph plot does not show up when program executed

我正在尝试学习如何使用 pyqtgraph 并尝试运行上面文档中给出的以下第一个简单示例:

#!/usr/bin/env python3

import pyqtgraph as pg

import numpy as np

x = np.random.normal(size=1000)

y = np.random.normal(size=1000)

pg.plot(x,y,pen=None,symbol='o',title='first graph')

我在 Raspberry Pi 3 上使用 python 3.5.3 Raspbian Stretch。

如果我运行在Thonny或IDLE中运行上述程序,程序运行没有任何错误但不显示任何输出。

同样,如果我在 Linux 命令提示符下 运行 程序,只需调用程序名称(我已使用 chmod +x 使其可执行)或键入 python3 后跟程序名称,仍然没有显示任何内容。

但是,如果我在 Linux 提示符下键入 python3 并得到 python 提示符,然后 运行 程序中的每一行,然后它会按预期在标题为 "first graph" 的 window 中显示一个散点图。

有人可以让我知道在 运行 通过 Thonny 或 IDLE 或将其作为程序调用时,我需要做什么来获取显示图形的代码吗?

谢谢。

每个 GUI 都需要一个事件循环,在您的情况下您没有创建它,在下面的代码中我展示了如何做:

#!/usr/bin/env python3

import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np


x = np.random.normal(size=1000)
y = np.random.normal(size=1000)
pg.plot(x,y,pen=None,symbol='o',title='first graph')


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

注意:不要使用任何IDE,因为许多不能正确处理事件循环,从终端执行它。