from PyQt4 import QtGui, QtCore class MyRect(QtGui.QGraphicsRectItem): def __init__(self, parent=None): super(MyRect, self).__init__(parent) self.setRect(0, 0, 100, 100) self.setBrush(QtGui.QBrush(QtCore.Qt.red)) class MyAnimation(QtGui.QGraphicsItemAnimation): def __init__(self, item): super(MyAnimation, self).__init__() self.setItem(item) self.setPosAt(0.0, QtCore.QPointF(0, 0)) self.setPosAt(1.0, QtCore.QPointF(300, 200)) self.setDuration(5000) app = QtGui.QApplication([]) scene = QtGui.QGraphicsScene() view = QtGui.QGraphicsView(scene) view.show() rect = MyRect() scene.addItem(rect) animation = MyAnimation(rect) animation.start() app.exec_()
from PyQt4 import QtGui, QtCore class MyPolygon(QtGui.QGraphicsPolygonItem): def __init__(self, parent=None): super(MyPolygon, self).__init__(parent) self.setPolygon(QtGui.QPolygonF([ QtCore.QPointF(0, 0), QtCore.QPointF(50, 50), QtCore.QPointF(100, 0) ])) self.setBrush(QtGui.QBrush(QtCore.Qt.blue)) class MyAnimation(QtGui.QGraphicsItemAnimation): def __init__(self, item): super(MyAnimation, self).__init__() self.setItem(item) self.setRotationAt(0.0, 0) self.setRotationAt(1.0, 360) self.setDuration(5000) app = QtGui.QApplication([]) scene = QtGui.QGraphicsScene() view = QtGui.QGraphicsView(scene) view.show() polygon = MyPolygon() scene.addItem(polygon) animation = MyAnimation(polygon) animation.start() app.exec_()In this example, a blue polygon with three points is added to a QGraphicsScene and the animation package is used to rotate the item 360 degrees over a period of 5000 milliseconds. Package library: PyQt4 QtGui QGraphicsItemAnimation is part of PyQt4's graphical user interface module (QtGui).