Modern GUI Animation using QPropertyAnimation with Python, Qt Designer, and PySide2 (or PyQt5) Widgets

Posted by


Introduction:

In this tutorial, we will learn how to create a modern graphical user interface (GUI) using Python, Qt Designer, and PySide2 (or PyQt5). We will focus on using QPropertyAnimation to create dynamic animations for our widgets, which will enhance the user experience of our application.
We will cover the following topics in this tutorial:

  1. Setting up your development environment
  2. Designing your GUI using Qt Designer
  3. Using PySide2 (or PyQt5) to load and interact with your GUI
  4. Using QPropertyAnimation to animate widgets in your GUI

Setting up your development environment:

Before we begin, make sure you have Python installed on your computer. You can download Python from the official website (https://www.python.org/). We will also need Qt Designer, which is part of the Qt development tools. You can download Qt Designer from the Qt website (https://www.qt.io/download).

Once you have Python and Qt Designer installed, you will need to install PySide2 (or PyQt5) using pip. Open a terminal window and run the following command:

pip install PySide2

or

pip install PyQt5

Designing your GUI using Qt Designer:

Open Qt Designer and create a new form using the "Main Window" template. Design your GUI layout by dragging and dropping widgets from the toolbox onto the form. For this tutorial, we will create a simple application with a button that animates when clicked.

Add a QPushButton widget to the form and set its text property to "Animate". Save your form as "mainwindow.ui".

Using PySide2 (or PyQt5) to load and interact with your GUI:

In your Python script, import the necessary modules:

import sys
from PySide2.QtWidgets import QApplication, QMainWindow
from PySide2.QtUiTools import QUiLoader

If you are using PyQt5 instead of PySide2, you can use the following imports:

from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.uic import loadUi

Create a class that inherits from QMainWindow:

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        loader = QUiLoader()
        self.ui = loader.load("mainwindow.ui", self)

If you are using PyQt5, you can load the UI file using the loadUi function instead:

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        loadUi("mainwindow.ui", self)

Instantiate the QApplication and MainWindow classes:

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

Run your script to see your GUI displayed with the "Animate" button. Next, we will add animation to the button using QPropertyAnimation.

Using QPropertyAnimation to animate widgets in your GUI:

Import the necessary module:

from PySide2.QtCore import QPropertyAnimation

If you are using PyQt5, you can import QPropertyAnimation from the PyQt5.QtCore module instead.

Add the following method to the MainWindow class to animate the button:

def animate_button(self):
    animation = QPropertyAnimation(self.ui.button, b"geometry")
    animation.setDuration(1000)
    animation.setStartValue(self.ui.button.geometry())
    animation.setEndValue(self.ui.button.geometry().adjusted(200, 0, 200, 0))
    animation.start()

Connect the "clicked" signal of the button to the animate_button method in the MainWindow class:

self.ui.button.clicked.connect(self.animate_button)

Run your script and click the "Animate" button to see it move to the right. You can customize the animation by adjusting the duration, start value, and end value properties of the QPropertyAnimation.

Conclusion:

In this tutorial, we have learned how to create a modern graphical user interface using Python, Qt Designer, and PySide2 (or PyQt5). We have demonstrated how to use QPropertyAnimation to animate widgets in our GUI, enhancing the user experience of our application. Experiment with different animations and customize them to suit your design preferences. The possibilities are endless, so have fun exploring and creating dynamic GUIs with Python and Qt!

0 0 votes
Article Rating

Leave a Reply

30 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@WandersonItsMe
3 hours ago

Because some problems with copyright of music some pieces are without sound.

Unfortunately, some people release music without copyright and right after having a lot of people using it, they apply copyright.

@promanplayer8064
3 hours ago

Pls plus widow size. I just watch your video with my phone

@marcospiotto9755
3 hours ago

Nice video! How did you get the View Python Code botton in Qt designer?

@ice-code4431
3 hours ago

Thank you very much, really great, please post more videos [qt]

Are these libraries the same for pyqt5?

@NailujAgelliv
3 hours ago

Não tenho muita experiência com Python e fiquei com a seguinte duvida:
Na linha "self.animation = QPropertyAnimation(self.ui.progressBar , b"value")" eu não sei o que o 2° parâmetro é.
A documentação da clase QPropertyAnimation diz que o 2° parâmetro deve ser uma instânciada clase PySide6.QtCore.QByteArray , eu não entendo como b"value" retorna uma instância dessa clase, b é algum tipo de construtor?

Desde já, muito obrigado.

@DaniruDEV
3 hours ago

first thing i learnt from u is not only the features make your app greate but also the colors. Your colors selection is awsome

@maumau4369
3 hours ago

so my problem is when I will call the class MainWindow(QMainWindow) in the if _name_ == '__main__': thingi I get an error Because the class have an argument and yeah but when I put in an argument the int cant take it so please help me 🙁

@mrrohan6396
3 hours ago

Hi,
Is it possible to create an animation of elevator going up and down using pyqt?
I got this idea after watching your content. Is it possible though?

@andersonlemos572
3 hours ago

how can I make to output print wich letter wit different colors?
Ex: if I put inside the input de letter "a" output print "b" RED
if i put "c" output print "z" blue
and, if write a text and my text have the letters "b" and "z"… b will print RED, and z will print blue like "BAZINGA"

@galaxymariosuper
3 hours ago

so good man…
btw you dont need to setStart value() to any values. that way it can be retracted and expanded at any state

@FireSecret
3 hours ago

9:55 How do you get all the info for the class and the methods available for each class? I really want to have this on my VS

@josealexsandro9768
3 hours ago

Qual é a diferença só Qt Desing Para O Qt Design Studio? Vídeo Muito Bom.

@ocfbnj
3 hours ago

Very good!

@jamesmacbeth7433
3 hours ago

What VSCode theme are you using?

@amjad88aa
3 hours ago

Real great i downloaded the repo even before this video . Couldnt wait 😊 But the video is even amazing . TYSM for your tutorials

@antonyalen2745
3 hours ago

More people should be aware of your channel coz you have premium content bro

@sportKO12
3 hours ago

Yre the Best Bro.

@etdle7
3 hours ago

I saw your recent posts that You make addons for Blender, also I make addons , but I see that You can create your own UI for that, It Will be awesome if You touch that topic un your videos, I'll appreciate your content, I love it!

@lassinaouattara1213
3 hours ago

More vidéo on pyqt or pyside please? Why not start building a application from the bigining till the end, for example restaurant management app,school management or any other app, that will be great sir 😉, thanks a lot for all those amazing tuto

@avaze7
3 hours ago

Hey I had a question: Im making an Program where I want to Pick colors, but I really dont want to use the native color picker as it totally doesn't fit the style. So after some experimenting I got a quite nice color picker, but I feel my Code is not really efficient and it lacks functionality like inputting hex colors & picking a screen color. Maybe you could do a tutorial about it? or maybe I could send you my Code and you can adapt and improve it to show case here?
I know that's a lot im asking, but youre the only Person I know that could do this good 🙂

30
0
Would love your thoughts, please comment.x
()
x