In this tutorial, we will create an animated side menu using PySide (the official Python bindings for Qt) and PyQt, implementing a modern UI with a drop-down menu. This side menu will make your interface more user-friendly and visually appealing.
To get started, make sure you have PySide and PyQt installed. You can install these packages using pip:
pip install PySide6 PyQt6
Now, let’s create a new Python file called animated_side_menu.py
and import the necessary modules:
from PySide6.QtWidgets import *
from PySide6.QtCore import *
from PySide6.QtGui import *
Next, let’s create a class for our main window where we will add the side menu. This class will inherit from QMainWindow
:
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Animated Side Menu")
self.setGeometry(100, 100, 800, 600)
self.initUI()
def initUI(self):
self.sidebar = QFrame()
self.layout = QVBoxLayout()
self.layout.setContentsMargins(0, 0, 0, 0)
self.sidebar.setLayout(self.layout)
self.setCentralWidget(self.sidebar)
In the initUI
method, we create a QFrame
to hold our side menu, and we set the layout of the frame to a QVBoxLayout
.
Now, let’s add some items to the side menu. We will create a custom widget for each item that will include an icon and a label. Here’s how you can create a custom item widget:
class MenuItem(QWidget):
def __init__(self, icon, text):
super().__init__()
self.initUI(icon, text)
def initUI(self, icon, text):
layout = QHBoxLayout()
layout.setContentsMargins(10, 0, 10, 0)
self.icon = QLabel()
self.icon.setPixmap(QPixmap(icon))
layout.addWidget(self.icon)
self.label = QLabel(text)
layout.addWidget(self.label)
self.setLayout(layout)
Now, let’s add some items to the side menu in the initUI
method of the MainWindow
class:
self.sidebar.layout().addWidget(MenuItem("icon.png", "Home"))
self.sidebar.layout().addWidget(MenuItem("icon.png", "About"))
self.sidebar.layout().addWidget(MenuItem("icon.png", "Settings"))
We can add as many items as we want to the side menu by calling addWidget
on the layout of the sidebar.
Next, let’s animate the side menu to slide in and out when the user clicks on a button. We will add a button at the top to toggle the menu:
self.toggleButton = QPushButton("☰")
self.toggleButton.clicked.connect(self.toggleMenu)
self.toggleButton.setFixedWidth(30)
self.layout.addWidget(self.toggleButton)
Now, let’s implement the toggleMenu
method to animate the side menu:
def toggleMenu(self):
width = self.sidebar.width()
if width == 0:
for i in range(1, 351, 50):
self.sidebar.setFixedWidth(i)
else:
for i in range(350, 0, -50):
self.sidebar.setFixedWidth(i)
In the toggleMenu
method, we check the current width of the sidebar. If the width is zero, we gradually increase the width to slide the menu in. If the width is already 350 (the maximum width we set), we gradually decrease the width to slide the menu out.
Finally, let’s create an instance of MainWindow
and show the window:
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
app.exec()
Now you can run the animated_side_menu.py
file, and you should see a modern-looking side menu with a drop-down animation when you click the toggle button.
This tutorial covered the basics of creating an animated side menu using PySide and PyQt. You can customize the design and add more functionality to make your interface even more interactive and user-friendly. Happy coding!
Is it possible to get the dark theme of QtDesigner in Window just like we get in Linux ?? If yes, then how can I do that ?
Amazing!! Superb sir,
SIr I have a request.
Please made a tutorial on how to make a scrollable window in pyqt5 so that I can add as much widget as I want in a single screen and the user will just scroll down to see them.
Could you please tell me where to learn this things
Bro are doing amazing things in python and thanx to giving us ❤️