哦,天哪……我刚刚想通了!我对SO的第一个回答,不要对我太苛刻。
首先,确保您在类的构造函数(init 函数)中创建 pyqtgraph 图。在那里调用一次并立即隐藏它(这对我来说是复杂的部分)。
这是一个示例代码:
import numpy as np
import pyqtgraph as pg
import pyqtgraph.exporters
class MyPlotClass():
def __init__(self):
self.windowplt = pg.plot()
self.windowplt.win.hide()
def savePlots(self):
x = np.arange(0, 256)
y = np.arange(0, 256)
self.windowplt.plot(x, y)
exporter = pg.exporters.ImageExporter(self.windowplt.plotItem)
exporter.params.param('width').setValue(256, blockSignal=exporter.widthChanged)
exporter.params.param('height').setValue(256, blockSignal=exporter.heightChanged)
for i in np.arange(0,10):
exporter.export('./fileName' + str(i) + '.png')
print(i)
if __name__ == "__main__":
saveMyFiles = MyPlotClass()
saveMyFiles.savePlots()
只有一个窗口会在拍摄期间出现并立即隐藏。
我知道您的问题很旧,但将来可能会对任何人有所帮助。我现在整天都在寻找解决方案。
正如您在上一个线程pyqtgraph for plotting multiple data lists 中提到的那样,ImageExporter.py 错误仍然存在。如果要更改 pyqtgraph 库的代码,您可以通过自己设置宽度和高度来解决它(如上面的代码中所示)。
exporter.params.param('width').setValue(256, blockSignal=exporter.widthChanged)
exporter.params.param('height').setValue(256, blockSignal=exporter.heightChanged)