In this PyQt5 article i want to show you Creating Animation with QPropertyAnimation. so first of all let’s talk about QPropertyAnimation.
What is QPropertyAnimation?
QPropertyAnimation class animates Qt properties. QPropertyAnimation interpolates over Qt properties. this class inherits QVariantAnimation, and supports animation of the same meta types as its super class. A class declaring properties must be a QObject. To make it possible to animate a property, it must provide a setter (so that QPropertyAnimation can set the property’s value). Note that this makes it possible to animate many of Qt’s widgets.
This is the complete code for this article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QFrame from PyQt5.QtCore import QPropertyAnimation, QRect import sys class Window(QMainWindow): def __init__(self): super().__init__() # Window properties self.title = "Codeloop.org - QPropertyAnimation" self.top = 100 self.left = 100 self.width = 680 self.height = 500 # Initialize window self.InitWindow() def InitWindow(self): # Set window properties self.setWindowIcon(QtGui.QIcon("codeloop.png")) self.setWindowTitle(self.title) self.setGeometry(self.top, self.left, self.width, self.height) # Create button self.button = QPushButton("Start", self) self.button.move(30, 30) self.button.clicked.connect(self.doAnimation) # Create frame self.frame = QFrame(self) self.frame.setFrameStyle(QFrame.Panel | QFrame.Raised) self.frame.setGeometry(150, 30, 100, 100) # Show window self.show() def doAnimation(self): # Create animation self.anim = QPropertyAnimation(self.frame, b"geometry") # Duration in milliseconds (10 seconds) self.anim.setDuration(10000) # Start position and size self.anim.setStartValue(QRect(0, 0, 100, 30)) # End position and size self.anim.setEndValue(QRect(250, 250, 100, 30)) # Start animation self.anim.start() # Create application instance App = QApplication(sys.argv) # Create window instance window = Window() # Start application event loop sys.exit(App.exec()) |
In here we are going to create our main Window class that extends from QMainWindow. and in the constructor of the class we need to add some requirements of the window like set window title, window icon and window geometry.
1 2 3 4 5 6 7 8 9 10 11 12 |
class Window(QMainWindow): def __init__(self): super().__init__() self.title = "Codeloop.org - QPropertyAnimation" self.top = 100 self.left = 100 self.width = 680 self.height = 500 self.InitWindow() |
In the InitWindow() method we set our window title, window icon and window geometry, also we create a button and a frame in this method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
def InitWindow(self): self.setWindowIcon(QtGui.QIcon("codeloop.png")) self.setWindowTitle(self.title) self.setGeometry(self.top, self.left, self.width, self.height) self.button = QPushButton("Start", self) self.button.move(30, 30) self.button.clicked.connect(self.doAnimation) self.frame = QFrame(self) self.frame.setFrameStyle(QFrame.Panel | QFrame.Raised) self.frame.setGeometry(150, 30, 100, 100) self.show() |
These lines of codes are for button creation also you can see that we have connected doAnimation() slot or method with clicked signal of button.
1 2 3 |
self.button = QPushButton("Start", self) self.button.move(30, 30) self.button.clicked.connect(self.doAnimation) |
This is our Frame creation.
1 2 3 |
self.frame = QFrame(self) self.frame.setFrameStyle(QFrame.Panel | QFrame.Raised) self.frame.setGeometry(150, 30, 100, 100) |
And this method is for creating Animation in PyQt5, in the first we have created the object of QPropertyAnimation. after that we have created the duration for our Animation and at the end we have started the animation.
1 2 3 4 5 6 |
def doAnimation(self): self.anim = QPropertyAnimation(self.frame, b"geometry") self.anim.setDuration(10000) self.anim.setStartValue(QRect(0, 0, 100, 30)) self.anim.setEndValue(QRect(250, 250, 100, 30)) self.anim.start() |
So run the code and this will be the result.
FAQs:
What is QPropertyAnimation in PyQt5?
QPropertyAnimation class animates Qt properties. QPropertyAnimation interpolates over Qt properties. this class inherits QVariantAnimation, and supports animation of the same meta types as its super class. A class declaring properties must be a QObject. To make it possible to animate a property, it must provide a setter (so that QPropertyAnimation can set the property’s value). Note that this makes it possible to animate many of Qt’s widgets.
What are the key features of the Window class in PyQt5?
Window class extends QMainWindow and initializes window properties such as title, icon, and geometry. It also contains a method called InitWindow() where the window title, icon, geometry, button and frame are set up.
How is the frame created?
The frame is created using QFrame. Its style is set to QFrame.Panel | QFrame.Raised, and its geometry is set using the setGeometry() method.
Subscribe and Get Free Video Courses & Articles in your Email