1

I have something like graph and i have a trouble. I need to draw snowman, which moves on cosine trajectory.

import sys
import math
import time
from PyQt4 import QtGui, QtCore
class MyFrame(QtGui.QGraphicsView):
    def __init__( self, parent = None ):
        super(MyFrame, self).__init__(parent)
        self.resize(800,600)
        scene = QtGui.QGraphicsScene()
        self.setScene(scene)

        for x in range(100):

            y = 100*(math.cos(x+200))

            pen1   = QtGui.QPen(QtGui.QColor(QtCore.Qt.red))
            brush1 = QtGui.QBrush(pen1.color().darker(255))
            item1 = scene.addEllipse(x, y, 100, 100, pen1,brush1)

            pen2   = QtGui.QPen(QtGui.QColor(QtCore.Qt.green))
            brush2 = QtGui.QBrush(pen2.color().darker(150))
            item2 = scene.addEllipse(x-25, y+100, 150, 150, pen2,brush2)

            pen3   = QtGui.QPen(QtGui.QColor(QtCore.Qt.blue))
            brush3 = QtGui.QBrush(pen3.color().darker(150))
            item3 = scene.addEllipse(x-50, y+250, 200, 200, pen3,brush3)
            time.sleep(3)



app = QtGui.QApplication([])
f = MyFrame()
f.show()
app.exec_()

Now i have some questions. When i use time.sleep(2), it's just wait for a few seconds and show me final result without steps. 1) What i need to do for fixing it? 2) What i need to do for situation, when it looks like moving animation? I tried to use QPoint, but it is a few hard to use in my code, i have no experience and i believe, that this problem solves more easier.

lwwwr
  • 49
  • 1
  • 10

1 Answers1

1

First, you are doing your animation in the __init__ method which means that the entire thing will complete before you've even shown the frame. Instead break this into a separate method you call after f.show().

Second, you need to throw a processEvents() call in during your animation to force qt to redraw the screen.

Third, you probably want to clear the scene in between drawings.

import sys
import math
import time
from PyQt4 import QtGui, QtCore
class MyFrame(QtGui.QGraphicsView):
    def __init__( self, parent = None ):
        super(MyFrame, self).__init__(parent)
        self.resize(800,600)
        self.scene = QtGui.QGraphicsScene()
        self.setScene(self.scene)

    def doAnimation(self):
        for x in range(5):

            self.scene.clear();
            y = 100*(math.cos(x+200))

            pen1   = QtGui.QPen(QtGui.QColor(QtCore.Qt.red))
            brush1 = QtGui.QBrush(pen1.color().darker(255))
            item1 = self.scene.addEllipse(x, y, 100, 100, pen1,brush1)

            pen2   = QtGui.QPen(QtGui.QColor(QtCore.Qt.green))
            brush2 = QtGui.QBrush(pen2.color().darker(150))
            item2 = self.scene.addEllipse(x-25, y+100, 150, 150, pen2,brush2)

            pen3   = QtGui.QPen(QtGui.QColor(QtCore.Qt.blue))
            brush3 = QtGui.QBrush(pen3.color().darker(150))
            item3 = self.scene.addEllipse(x-50, y+250, 200, 200, pen3,brush3)

            QtGui.QApplication.processEvents()
            time.sleep(1)

app = QtGui.QApplication([])
f = MyFrame()
f.show()
f.doAnimation()
app.exec_()
Mark
  • 106,305
  • 20
  • 172
  • 230