1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| from PyQt4.QtGui import *
from PyQt4.QtCore import *
import random
import sys
dx = 4
i = 0
def repeat():
global i, dx, rect_1, rect_2, rect_3, scene
i += 1
rect_1.setRect(10 + dx, 15, 90 - 2*dx, 60)
dx = -dx
rgb = [random.randint(0, 255) for _ in range(3)]
color = QColor(*rgb)
brush = QBrush(color)
rect_3.setBrush(brush)
if i == 3:
scene.removeItem(rect_2)
racine = QApplication(sys.argv)
scene = QGraphicsScene()
color = QColor(0, 0, 0)
brush = QBrush(color)
color = QColor(255, 0, 0)
pen = QPen(color)
rect_1 = scene.addRect(10, 15, 90, 60, pen = pen, brush = brush)
color = QColor(0, 255, 0)
brush = QBrush(color)
rect_2 = scene.addRect(130, 15, 90, 60, brush = brush)
color = QColor(0, 0, 255)
brush = QBrush(color)
rect_3 = scene.addRect(250, 15, 90, 60, brush = brush)
timer = QTimer()
timer.timeout.connect(repeat)
timer.start(500)
view = QGraphicsView(scene)
view.show()
racine.exec_() |
Partager