import sys from PyQt5.Qt import * class Window(QWidget): def __init__(self): super().__init__() self.setup_ui() def setup_ui(self): self.setWindowTitle("Qt桌面应用程序") self.resize(600, 600) self.push_button_1 = QPushButton("暂停", self) self.push_button_1.setStyleSheet("background-color:red") self.push_button_1.resize(100, 100) self.push_button_1.move(150, 150) self.push_button_2 = QPushButton("继续", self) self.push_button_2.resize(100, 100) self.push_button_2.setStyleSheet("background-color:green") self.label_1 = QLabel(self) self.label_1.move(200, 0) self.label_2 = QLabel(self) self.label_2.move(200, 15) self.set_animation() self.push_button_1.clicked.connect(self.animation_group.pause) self.push_button_2.clicked.connect(self.animation_group.resume) def set_animation(self): self.animation_group = QParallelAnimationGroup(self) self.animation_push_button_1 = QPropertyAnimation(self.push_button_1, b"pos", self) self.animation_push_button_1.setKeyValueAt(0, QPoint(150, 150)) self.animation_push_button_1.setKeyValueAt(0.25, QPoint(350, 150)) self.animation_push_button_1.setKeyValueAt(0.5, QPoint(350, 350)) self.animation_push_button_1.setKeyValueAt(0.75, QPoint(150, 350)) self.animation_push_button_1.setKeyValueAt(1, QPoint(150, 150)) self.animation_push_button_1.setDuration(5000) self.animation_push_button_2 = QPropertyAnimation(self.push_button_2, b"pos", self) self.animation_push_button_2.setKeyValueAt(0, QPoint(0, 0)) self.animation_push_button_2.setKeyValueAt(0.25, QPoint(0, 500)) self.animation_push_button_2.setKeyValueAt(0.5, QPoint(500, 500)) self.animation_push_button_2.setKeyValueAt(0.75, QPoint(500, 0)) self.animation_push_button_2.setKeyValueAt(1, QPoint(0, 0)) self.animation_push_button_2.setDuration(5000) self.animation_push_button_2.setEasingCurve(QEasingCurve.InBounce) # self.animation_push_button_2.setDirection(QAbstractAnimation.Backward) self.animation_group.addAnimation(self.animation_push_button_1) self.animation_group.addAnimation(self.animation_push_button_2) self.animation_group.setLoopCount(3) self.animation_group.currentLoopChanged.connect(lambda value: self.label_1.setText( f"循环:{self.animation_group.currentLoop() + 1} / 共{self.animation_group.loopCount()}次")) self.animation_group.stateChanged.connect( lambda value1, value2: self.label_2.setText(f"状态:新{value1} 旧{value2}")) self.animation_group.start() self.label_1.setText(f"循环:{self.animation_group.currentLoop() + 1} / 共{self.animation_group.loopCount()}次") if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())