Hi,
You just need to update the image data for the ImageItem (your code keeps
adding many new ImageItems to the scene!). Change:
img = pg.ImageItem(screen)
view.addItem(img)
to
img.setImage(screen)
and it will run a lot faster.
(Not related to your issue, but ultimately you'd like to put all this in,
say, an AntDemo class extending a QWidget or similar. Then you can stop
using global variables which are not the best way to be passing data
around. But for a simple example like this, globals are easy.)
Patrick
On Sunday, 27 May 2018 12:09:07 UTC+9:30, JSwift2046 wrote:
>
> Hi, sorry for such a beginner's question, but I've only been using Python
> for a couple of weeks, and almost entirely figuring out Keras.
>
> But I already love Pyqt, and thanks for developing such a great package. I
> think this will be a really simple one, but could anyone advise a sensible
> way to write this?
>
> This runs a simple animated Langton's Ant demo, but very slowly, because
> every update I'm using view.addItem – which is surely not the best way to
> plot real-time pixels. But very simple to write and look at. Thanks so much
> – this has been driving me slightly crazy.
>
> from PyQt5.QtGui import *
> from PyQt5.QtCore import *
> from PyQt5.QtWidgets import *
> from pyqtgraph.Qt import QtCore, QtGui
> import numpy as np
> import pyqtgraph as pg
> import sys
>
> direction = 1
> x = 250
> y = 250
>
> app = QtGui.QApplication([])
>
> w = pg.GraphicsView()
> w.show()
> w.resize(500,500)
> w.setWindowTitle('Ant demo')
>
> view = pg.ViewBox()
> w.setCentralItem(view)
> view.setAspectLocked(True)
>
> screen = np.zeros((500,500))
> img = pg.ImageItem(screen)
> view.addItem(img)
>
> img.setLevels([0, 1])
>
> def update():
> global screen, img, x, y, direction, w
>
> hello = screen[x,y]
>
> if hello == 1:
> direction += 1
> screen[x,y] = 0
> else:
> direction -= 1
> screen[x,y] = 1
>
> if direction == 0:
> direction = 4
> if direction == 5:
> direction = 1
>
> if direction == 1:
> y -= 1
> elif direction == 2:
> x += 1
> elif direction == 3:
> y += 1
> elif direction == 4:
> x -= 1
>
> img = pg.ImageItem(screen)
> view.addItem(img)
>
> timer = QtCore.QTimer()
> timer.timeout.connect(update)
> timer.start(1)
>
> if __name__ == '__main__':
> import sys
> if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
> QtGui.QApplication.instance().exec_()
>
>
--
You received this message because you are subscribed to the Google Groups
"pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/pyqtgraph/78d2d545-75ce-49ca-a12a-00619a49e81f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.