class Robot(RobotPart): def __init__(self): super(Robot, self).__init__() self.torsoItem = RobotTorso(self) self.headItem = RobotHead(self.torsoItem) self.upperLeftArmItem = RobotLimb(self.torsoItem) self.lowerLeftArmItem = RobotLimb(self.upperLeftArmItem) self.upperRightArmItem = RobotLimb(self.torsoItem) self.lowerRightArmItem = RobotLimb(self.upperRightArmItem) self.upperRightLegItem = RobotLimb(self.torsoItem) self.lowerRightLegItem = RobotLimb(self.upperRightLegItem) self.upperLeftLegItem = RobotLimb(self.torsoItem) self.lowerLeftLegItem = RobotLimb(self.upperLeftLegItem) self.timeline = QTimeLine() settings = [ # item position rotation at # x y time 0 / 1 ( self.headItem, 0, -18, 20, -20 ), ( self.upperLeftArmItem, -15, -10, 190, 180 ), ( self.lowerLeftArmItem, 30, 0, 50, 10 ), ( self.upperRightArmItem, 15, -10, 300, 310 ), ( self.lowerRightArmItem, 30, 0, 0, -70 ), ( self.upperRightLegItem, 10, 32, 40, 120 ), ( self.lowerRightLegItem, 30, 0, 10, 50 ), ( self.upperLeftLegItem, -10, 32, 150, 80 ), ( self.lowerLeftLegItem, 30, 0, 70, 10 ), ( self.torsoItem, 0, 0, 5, -20 ) ] self.animations = [] for item, pos_x, pos_y, rotation1, rotation2 in settings: item.setPos(pos_x,pos_y) animation = QGraphicsItemAnimation() animation.setItem(item) animation.setTimeLine(self.timeline) animation.setRotationAt(0, rotation1) animation.setRotationAt(1, rotation2) self.animations.append(animation) self.animations[0].setScaleAt(1, 1.1, 1.1) self.timeline.setUpdateInterval(1000 / 25) self.timeline.setCurveShape(QTimeLine.SineCurve) self.timeline.setLoopCount(0) self.timeline.setDuration(2000) self.timeline.start() def boundingRect(self): return QRectF() def paint(self, painter, option, widget=None): pass
class Robot(RobotPart): def __init__(self): super(Robot, self).__init__() self.torsoItem = RobotTorso(self) self.headItem = RobotHead(self.torsoItem) self.upperLeftArmItem = RobotLimb(self.torsoItem) self.lowerLeftArmItem = RobotLimb(self.upperLeftArmItem) self.upperRightArmItem = RobotLimb(self.torsoItem) self.lowerRightArmItem = RobotLimb(self.upperRightArmItem) self.upperRightLegItem = RobotLimb(self.torsoItem) self.lowerRightLegItem = RobotLimb(self.upperRightLegItem) self.upperLeftLegItem = RobotLimb(self.torsoItem) self.lowerLeftLegItem = RobotLimb(self.upperLeftLegItem) self.timeline = QTimeLine() settings = [ # item position rotation at # x y time 0 / 1 (self.headItem, 0, -18, 20, -20), (self.upperLeftArmItem, -15, -10, 190, 180), (self.lowerLeftArmItem, 30, 0, 50, 10), (self.upperRightArmItem, 15, -10, 300, 310), (self.lowerRightArmItem, 30, 0, 0, -70), (self.upperRightLegItem, 10, 32, 40, 120), (self.lowerRightLegItem, 30, 0, 10, 50), (self.upperLeftLegItem, -10, 32, 150, 80), (self.lowerLeftLegItem, 30, 0, 70, 10), (self.torsoItem, 0, 0, 5, -20) ] self.animations = [] for item, pos_x, pos_y, rotation1, rotation2 in settings: item.setPos(pos_x, pos_y) animation = QGraphicsItemAnimation() animation.setItem(item) animation.setTimeLine(self.timeline) animation.setRotationAt(0, rotation1) animation.setRotationAt(1, rotation2) self.animations.append(animation) self.animations[0].setScaleAt(1, 1.1, 1.1) self.timeline.setUpdateInterval(1000 / 25) self.timeline.setCurveShape(QTimeLine.SineCurve) self.timeline.setLoopCount(0) self.timeline.setDuration(2000) self.timeline.start() def boundingRect(self): return QRectF() def paint(self, painter, option, widget=None): pass
class MyView(QGraphicsView): def __init__(self): super().__init__() self.initView() self.setupScene() self.setupAnimation() self.setGeometry(300, 150, 250, 250) def initView(self): self.setWindowTitle("Progress meter") self.setRenderHint(QPainter.Antialiasing) policy = Qt.ScrollBarAlwaysOff self.setVerticalScrollBarPolicy(policy) self.setHorizontalScrollBarPolicy(policy) self.setBackgroundBrush(self.palette().window()) self.pm = ProgressMeter(self) self.pm.setPos(55, 55) def setupScene(self): self.scene = QGraphicsScene(self) self.scene.setSceneRect(0, 0, 250, 250) self.scene.addItem(self.pm) self.setScene(self.scene) def setupAnimation(self): self.timer = QTimeLine() self.timer.setLoopCount(0) self.timer.setFrameRange(0, 100) self.timer.frameChanged[int].connect(self.doStep) self.timer.start() def doStep(self, i): if not self.pm.increment(): self.timer.stop() self.pm.update()
class AnimationView(QWidget): """docstring for AnimationView""" frameIndex = 0 frameCount = 0 def __init__(self, bmpFrames): super(AnimationView, self).__init__() self.setFixedSize(frameWidth,frameHeight) self.bmpSize =64 if bmpFrames : self.bmpFrames = bmpFrames self.frameCount = len(bmpFrames) self.bmpSize = bmpFrames[0].width() else : self.initDefaultFrames() def initDefaultFrames(self): self.bmpFrames = [] defaultImage = QImage("..\\resource\\default.bmp") imageWidth = defaultImage.width() imageHeight = defaultImage.height() #判断各幀图片是否是横向排列 isHorizontal = min(imageWidth,imageHeight) == imageHeight #计算幀数 self.frameCount = imageWidth//frameWidth if isHorizontal else imageHeight//frameHeight for i in range(0,self.frameCount): pixmap = QPixmap(defaultImage.copy(i*frameWidth if isHorizontal else 0, 0 if isHorizontal else i*frameHeight,frameWidth,frameHeight)) eliminateBackgroundColor(pixmap) self.bmpFrames.append(pixmap) def createAnimation(self): self.timeLine = QTimeLine(10*1000) self.timeLine.setFrameRange(0,60//(4/self.frameCount)) self.timeLine.frameChanged.connect(self.refreshFrameIndex) self.timeLine.setLoopCount(0) self.timeLine.setCurveShape(3) self.timeLine.start() def refreshFrameIndex(self,currFrame): self.update() self.frameIndex = (self.frameIndex+1) % self.frameCount def paintEvent(self,event): painter = QPainter(self) painter.drawPixmap((frameWidth-self.bmpSize)//2,(frameHeight-self.bmpSize)//2,self.bmpFrames[self.frameIndex])
class Demo(QWidget): def __init__(self): super(Demo, self).__init__() self.resize(600, 600) self.label = QLabel('Hello PyQt5', self) self.label.move(-100, 100) self.timeline = QTimeLine(5000, self) self.timeline.setFrameRange(0, 700) self.timeline.frameChanged.connect(self.set_frame_func) self.timeline.stateChanged.connect( lambda: print(self.timeline.state())) # 1 self.timeline.setLoopCount(0) self.start_btn = QPushButton('Start', self) self.stop_btn = QPushButton('Stop', self) self.pause_btn = QPushButton('Pause', self) self.resume_btn = QPushButton('Resume', self) self.start_btn.clicked.connect(self.timeline.start) # 2 self.stop_btn.clicked.connect(self.timeline.stop) self.pause_btn.clicked.connect(lambda: self.timeline.setPaused(True)) self.resume_btn.clicked.connect(self.timeline.resume) self.h_layout = QHBoxLayout() self.v_layout = QVBoxLayout() self.h_layout.addWidget(self.start_btn) self.h_layout.addWidget(self.stop_btn) self.h_layout.addWidget(self.pause_btn) self.h_layout.addWidget(self.resume_btn) self.v_layout.addStretch(1) self.v_layout.addLayout(self.h_layout) self.setLayout(self.v_layout) def set_frame_func(self, frame): self.label.move(-100 + frame, 100)