In this tutorial, we will learn how to build and animate buttons and icons in Python using PyQt or PySide. We will specifically focus on using the QPushButtons class to create buttons with custom icons and implement animations to make the user interface more interactive and visually appealing.
To begin, make sure you have PyQt5 or PySide installed on your machine. You can install them using pip:
pip install PyQt5
or
pip install PySide2
Now, let’s get started with building our button with a custom icon. We will first import the necessary modules:
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import QSize
import sys
Next, create a class for our main window and define the init method to set up the window:
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Button Animation Example")
self.setGeometry(100, 100, 800, 600)
Now, let’s create a method to add a QPushButton with a custom icon to our window:
def add_button(self):
button = QPushButton(self)
button.setIcon(QIcon("icon.png")) # Replace "icon.png" with the path to your custom icon
button.setIconSize(QSize(50, 50)) # Set the size of the icon
button.setGeometry(100, 100, 50, 50) # Set the position and size of the button
We can now call the add_button method in the init method to add the button to our window:
self.add_button()
Next, let’s animate the button when it is clicked. We will use PyQt’s animation framework for this. Import the necessary module for animation:
from PyQt5.QtCore import QPropertyAnimation
Then, define a method to animate the button when it is clicked:
def animate_button(self, button):
animation = QPropertyAnimation(button, b"geometry")
animation.setDuration(1000) # Set the duration of the animation in milliseconds
animation.setStartValue(button.geometry())
animation.setEndValue(button.geometry().adjusted(50, 50, -50, -50)) # Adjust the position and size of the button
animation.start()
Now, connect the animate_button method to the clicked event of the button:
button.clicked.connect(lambda: self.animate_button(button))
Finally, create an instance of QApplication and MainWindow, and start the application:
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
And that’s it! You have now created a PyQt or PySide application with a button that has a custom icon and an animation that triggers when the button is clicked. You can further customize the button and animation to suit your needs and create a modern UI with interactive elements.
i'm having a probleme Loading my svg icone can you help ?
iconify.path.IconNotFoundError: Unable to locate icon file: /icons/hi/kakashi_16.svg
Json file
{
"QPushButton": [
{
"name": "pushButton_1",
"iconify": [
{
"icon": "/icons/hi/kakashi_16.svg",
"size": 32,
"animation": "breathe",
"animateOn": "hover"
}
]
}
]
}
AttributeError: 'QPushButton' object has no attribute 'setObjectCustomTheme'
Good day,
I was adding the iconfiy feature to an existing project and i'm using [ loadJsonStyle(self, self.ui) ] to load my style.json file but it failed to load after adding
[ from custom_buttons import * ] to my code. this was the erro displayed : "loadJsonStyle(self, self.ui)
TypeError: loadJsonStyle() takes 1 positional argument but 2 were given"
Very helpful
Thank you my friend, very nice.
There is something you want to ask.
How can we add a swf file as an icon on QPushButton for animated button?
Hello friend, do you have any interface that you use database?
bugs with applyShadow –> position of widget is changed when dragged after triggering shadow animaiton !
Can you start from scratch because I don't understand?
Amazing and wonderfull, thank you
Sorry guys, I recorded this video late at night so I didn't explain the whole process well. I will make small parts of this video where I will try to explain more.
Watch here https://www.youtube.com/watch?v=pBdXQ8XwwYM&list=PLJ8t3BKaQLhOlIuZ8psjRvWQ5S5OBQ6iA
amazing as usual