pyqtgraph/examples/ImageItem.py
KIU Shueng Chuan 85e894dd73 convert uses of QTimer.singleShot to QTimer instances
On macOS and Linux with PyQt bindings, QTimer.singleShot continues
to fire on python interpreter shutdown.
2021-03-02 05:57:17 +08:00

66 lines
1.6 KiB
Python

# -*- coding: utf-8 -*-
"""
Demonstrates very basic use of ImageItem to display image data inside a ViewBox.
"""
## Add path to library (just for examples; you do not need this)
import initExample
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
import pyqtgraph as pg
import pyqtgraph.ptime as ptime
app = pg.mkQApp("ImageItem Example")
## Create window with GraphicsView widget
win = pg.GraphicsLayoutWidget()
win.show() ## show widget alone in its own window
win.setWindowTitle('pyqtgraph example: ImageItem')
view = win.addViewBox()
## lock the aspect ratio so pixels are always square
view.setAspectLocked(True)
## Create image item
img = pg.ImageItem(border='w')
view.addItem(img)
## Set initial view bounds
view.setRange(QtCore.QRectF(0, 0, 600, 600))
## Create random image
data = np.random.normal(size=(15, 600, 600), loc=1024, scale=64).astype(np.uint16)
i = 0
updateTime = ptime.time()
fps = 0
timer = QtCore.QTimer()
timer.setSingleShot(True)
# not using QTimer.singleShot() because of persistence on PyQt. see PR #1605
def updateData():
global img, data, i, updateTime, fps
## Display the data
img.setImage(data[i])
i = (i+1) % data.shape[0]
timer.start(1)
now = ptime.time()
fps2 = 1.0 / (now-updateTime)
updateTime = now
fps = fps * 0.9 + fps2 * 0.1
#print "%0.1f fps" % fps
timer.timeout.connect(updateData)
updateData()
## 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_()