运行 PyQt5 中同时出现两个动画

Run two animations simultaneously in PyQt5

我创建了两个单独的函数来为我的标题图片制作动画。一个让它稍微向下移动,另一个让它淡入(增加不透明度)。它们都独立工作,但一起工作时,只有不透明度实际上是 运行。我如何让它们同时 运行?

运动函数:

def title_intro(self):
        self.animation = QtCore.QPropertyAnimation(self.ui.title_image, b"geometry")
        self.animation.setDuration(500)
        self.animation.setStartValue(QtCore.QRect(190, -60, 671, 261))
        self.animation.setEndValue(QtCore.QRect(190, 0, 671, 261))
        self.animation.start()

不透明度函数:

def title_intro_opacity(self):
        self.opacity_effect = QtWidgets.QGraphicsOpacityEffect()
        self.ui.title_image.setGraphicsEffect(self.opacity_effect)

        self.animation = QtCore.QPropertyAnimation(self.opacity_effect, b"opacity")
        self.animation.setDuration(500)
        self.animation.setStartValue(0)
        self.animation.setEndValue(1)
        self.animation.start()

项目代码(上下文):

from PyQt5 import QtCore, QtGui, QtWidgets, QtQuick
import sys
import platform

from ui_main import Ui_MainWindow

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.title_intro()
        self.title_intro_opacity()
        self.show()

    def title_intro(self):
        self.animation = QtCore.QPropertyAnimation(self.ui.title_image, b"geometry")
        self.animation.setDuration(500)
        self.animation.setStartValue(QtCore.QRect(190, -60, 671, 261))
        self.animation.setEndValue(QtCore.QRect(190, 0, 671, 261))
        self.animation.start()
    
    def title_intro_opacity(self):
        self.opacity_effect = QtWidgets.QGraphicsOpacityEffect()
        self.ui.title_image.setGraphicsEffect(self.opacity_effect)

        self.animation = QtCore.QPropertyAnimation(self.opacity_effect, b"opacity")
        self.animation.setDuration(500)
        self.animation.setStartValue(0)
        self.animation.setEndValue(1)
        self.animation.start()

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec_())

整个Class:

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1070, 720)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("Game Graphics/CCM logo circle.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        MainWindow.setWindowIcon(icon)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.background_title_image = QtWidgets.QLabel(self.centralwidget)
        self.background_title_image.setGeometry(QtCore.QRect(0, 0, 1071, 741))
        self.background_title_image.setStyleSheet("border-image: url(:/newPrefix/background image.png);")
        self.background_title_image.setText("")
        self.background_title_image.setScaledContents(True)
        self.background_title_image.setObjectName("background_title_image")
        self.title_image = QtWidgets.QLabel(self.centralwidget)
        self.title_image.setGeometry(QtCore.QRect(190, 0, 671, 261))
        self.title_image.setAutoFillBackground(False)
        self.title_image.setStyleSheet("image: url(:/newPrefix/clash clan manager text.png);")
        self.title_image.setText("")
        self.title_image.setScaledContents(True)
        self.title_image.setAlignment(QtCore.Qt.AlignCenter)
        self.title_image.setObjectName("title_image")
        self.log_in_button = QtWidgets.QPushButton(self.centralwidget)
        self.log_in_button.setGeometry(QtCore.QRect(340, 330, 381, 81))
        font = QtGui.QFont()
        font.setFamily("Supercell-Magic")
        font.setPointSize(14)
        font.setBold(True)
        font.setItalic(False)
        font.setWeight(75)
        self.log_in_button.setFont(font)
        self.log_in_button.setAutoFillBackground(False)
        self.log_in_button.setStyleSheet("QPushButton {\n"
"    background-color: qlineargradient(spread:pad, x1:0.125, y1:0.471909, x2:0.938, y2:0.472, stop:0         rgba(49, 87, 255, 255), stop:1 rgba(38, 168, 255, 255)); \n"
"    color: white;\n"
"     border-radius: 10; \n"
"    padding: 6; \n"
"    border-style: outset;\n"
"    border-width: 1;\n"
"    border-top-color: rgb(135, 135, 135);\n"
"    border-left-color: rgb(135, 135, 135);\n"
"    border-right-color: rgb(0, 0, 0);\n"
"    border-bottom-color: rgb(0, 0, 0);\n"
"    \n"
"}\n"
"\n"
"QPushButton:pressed {\n"
"    border-style: outset;\n"
"    border-top-color: rgb(0, 0, 0);\n"
"    border-left-color: rgb(0, 0, 0);\n"
"    border-right-color: rgb(135, 135, 135);\n"
"    border-bottom-color: rgb(135, 135, 135);\n"
"    \n"
"}")
        self.log_in_button.setCheckable(False)
        self.log_in_button.setChecked(False)
        self.log_in_button.setDefault(False)
        self.log_in_button.setFlat(False)
        self.log_in_button.setObjectName("log_in_button")
        self.sign_up_button = QtWidgets.QPushButton(self.centralwidget)
        self.sign_up_button.setGeometry(QtCore.QRect(340, 430, 381, 81))
        font = QtGui.QFont()
        font.setFamily("Supercell-Magic")
        font.setPointSize(14)
        font.setBold(True)
        font.setItalic(False)
        font.setWeight(75)
        self.sign_up_button.setFont(font)
        self.sign_up_button.setAutoFillBackground(False)
        self.sign_up_button.setStyleSheet("QPushButton {\n"
"    background-color: qlineargradient(spread:pad, x1:0.125, y1:0.466, x2:0.938, y2:0.472, stop:0 rgba(0, 144, 24, 255), stop:1 rgba(124, 255, 38, 255));\n"
"    color: white;\n"
"     border-radius: 10; \n"
"    padding: 6; \n"
"    border-style: outset;\n"
"    border-width: 1;\n"
"    border-top-color: rgb(135, 135, 135);\n"
"    border-left-color: rgb(135, 135, 135);\n"
"    border-right-color: rgb(0, 0, 0);\n"
"    border-bottom-color: rgb(0, 0, 0);\n"
"    \n"
"}\n"
"\n"
"QPushButton:pressed {\n"
"    border-style: outset;\n"
"    border-top-color: rgb(0, 0, 0);\n"
"    border-left-color: rgb(0, 0, 0);\n"
"    border-right-color: rgb(135, 135, 135);\n"
"    border-bottom-color: rgb(135, 135, 135);\n"
"    \n"
"}")
        self.sign_up_button.setCheckable(False)
        self.sign_up_button.setChecked(False)
        self.sign_up_button.setDefault(False)
        self.sign_up_button.setFlat(False)
        self.sign_up_button.setObjectName("sign_up_button")
        self.background_title_image.raise_()
        self.title_image.raise_()
        self.sign_up_button.raise_()
        self.log_in_button.raise_()
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1070, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Clash Clan Manager"))
        self.log_in_button.setText(_translate("MainWindow", "LOG IN"))
        self.sign_up_button.setText(_translate("MainWindow", "SIGN UP"))
import source


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

问题是您将 2 个对象分配给同一个变量,因此前一个对象被消除了。在这种情况下,没有必要两者都是属性,还有一个改进是使用 QParallelAnimationGroup:

import sys

from PyQt5 import QtCore, QtGui, QtWidgets


from ui_main import Ui_MainWindow


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.start_animation()
        self.show()

    def start_animation(self):
        opacity_effect = QtWidgets.QGraphicsOpacityEffect(self.ui.title_image)
        self.ui.title_image.setGraphicsEffect(opacity_effect)

        geometry_animation = QtCore.QPropertyAnimation(
            self.ui.title_image,
            b"geometry",
            duration=500,
            startValue=QtCore.QRect(190, -60, 671, 261),
            endValue=QtCore.QRect(190, 0, 671, 261),
        )

        opacity_animation = QtCore.QPropertyAnimation(
            opacity_effect, b"opacity", duration=500, startValue=0.0, endValue=1.0
        )

        group = QtCore.QParallelAnimationGroup(self.ui.title_image)
        group.addAnimation(geometry_animation)
        group.addAnimation(opacity_animation)
        group.start(QtCore.QAbstractAnimation.DeleteWhenStopped)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec_())