pythonpyqt5qpropertyanimation

Generate Slide animation left wing when changing Sales


I have the following code in which I seek to generate an animation slide to the left and show the following QMainWindow and close the current one for which it uses a QPropertyAnimation within a function but it does not work here I leave the code:

Origin.py

from PyQt5.QtWidgets import QMainWindow,QApplication,QPushButton
from PyQt5 import QtCore
from segunda import MainTwo

class Main(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)


        self.Boton = QPushButton(self)
        self.Boton.setText("Press")
        self.Boton.clicked.connect(self.AnimaFunction)

        self.next = MainTwo()

    def AnimaFunction(self):
        self.anima = QtCore.QPropertyAnimation(self.next.show(),b'geometry')
        self.anima.setDuration(1000)
        self.anima.setStartValue(QtCore.QRect(0,0,0,0))
        self.anima.setEndValue(QtCore.QRect(self.next.geometry()))
        self.anima.start()


app = QApplication([])
m = Main()
m.show()
m.resize(800,600)
app.exec_()

Segunda.py

from PyQt5.QtWidgets import QMainWindow,QApplication,QLabel


class MainTwo(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)


        self.Label = QLabel(self)
        self.Label.setText("Soy la segunda ventana")
        self.Label.resize(200,200)

Solution

  • You must pass the window to the QPropertyAnimation, instead you are passing the return of the show method that is None so the QPropertyAnimation will not do its job, considering the above the solution is:

    def AnimaFunction(self):
        self.anima = QtCore.QPropertyAnimation(self.next, b'geometry')
        self.anima.setDuration(1000)
        self.anima.setStartValue(QtCore.QRect(0,0,0,0))
        self.anima.setEndValue(QtCore.QRect(self.next.geometry()))
        self.anima.start()
        self.next.show()
        self.hide()