SoFunction
Updated on 2025-05-14

Example of PyQt5 implementing smooth moving side menu bar effect

#!/usr/bin/env python
# encoding: utf-8
'''
@author: JHC 
@license: None
@contact: JHC000abc@
@file: 
@time: 2022/07/19/ 16:56
@desc:
'''
from  import Qt, QPropertyAnimation, QEasingCurve, QPoint, QPointF
from  import QMouseEvent
from  import QWidget, QApplication

class CDrawer(QWidget):

    LEFT, TOP, RIGHT, BOTTOM = range(4)

    def __init__(self, *args, stretch=1 / 3, direction=0, widget=None, **kwargs):
        super(CDrawer, self).__init__(*args, **kwargs)
        ((
        ) |  |  | )
        (Qt.WA_StyledBackground, True)
        (Qt.WA_TranslucentBackground, True)
        # Enter the animation         = QPropertyAnimation(
            self, duration=500, easingCurve=)
        (b'pos')
        # Leave the animation         = QPropertyAnimation(
            self, duration=500, finished=,
            easingCurve=)
        (b'pos')
        (500)
        (stretch)        #Percentage         = direction      # direction        # Translucent background         = QWidget(
            self, objectName='CDrawer_alphaWidget',
            styleSheet='#CDrawer_alphaWidget{background:rgba(55,55,55,100);}')
        (Qt.WA_TransparentForMouseEvents, True)
        (widget)          # Subcontrol
    def resizeEvent(self, event):
        (())
        super(CDrawer, self).resizeEvent(event)

    def mousePressEvent(self, event):
        pos = ()
        if () >= 0 and () >= 0 and (pos) == None and :
            if not ().contains(pos):
                ()
                return
        super(CDrawer, self).mousePressEvent(event)

    def show(self):
        super(CDrawer, self).show()
        parent = ().window() if () else ()
        if not parent or not :
            return
        # Set the Drawer size to the main window        (())
        geometry = ()
        (geometry)

    def animationIn(self, geometry):
        """Enter the animation
         :param geometry:
         """
        if  == :
            # left drawer            (
                0, 0, int(() * ), ())
            ()
            (QPoint(-(), 0))
            (QPoint(0, 0))
            ()
            ()
        elif  == :
            # upper drawer            (
                0, 0, (), int(() * ))
            ()
            (QPoint(0, -()))
            (QPoint(0, 0))
            ()
            ()
        elif  == :
            # right drawer            width = int(() * )
            (
                () - width, 0, width, ())
            ()
            (QPoint((), 0))
            (
                QPoint(() - (), 0))
            ()
            ()
        elif  == :
            # Drawer below            height = int(() * )
            (
                0, () - height, (), height)
            ()
            (QPoint(0, ()))
            (
                QPoint(0, () - ()))
            ()
            ()

    def animationOut(self):
        """Leave the animation
         """
        ()  # Stop entering the animation        geometry = ()
        if  == :
            # left drawer            (())
            (QPoint(-(), 0))
            ()
        elif  == :
            # upper drawer            (QPoint(0, ()))
            (QPoint(0, -()))
            ()
        elif  == :
            # right drawer            (QPoint((), 0))
            (QPoint((), 0))
            ()
        elif  == :
            # Drawer below            (QPoint(0, ()))
            (QPoint(0, ()))
            ()

    def onAnimOutEnd(self):
        """Leave the animation ends
         """
        # Simulate click outside to close        (self, QMouseEvent(
            , QPointF(-1, -1), , , ))

    def setWidget(self, widget):
        """Setting child controls
         :param widget:
         """
         = widget
        if widget:
            (self)
            (widget)
            (widget)

    def setEasingCurve(self, easingCurve):
        """Set an animation curve
         :param easingCurve:
         """
        (easingCurve)

    def getStretch(self):
        """Get proportion
         """
        return 

    def setStretch(self, stretch):
        """Set the proportion
         :param stretch:
         """
         = max(0.1, min(stretch, 0.9))

    def getDirection(self):
        """Get direction
         """
        return 

    def setDirection(self, direction):
        """Set the direction
         :param direction:
         """
        direction = int(direction)
        if direction < 0 or direction > 3:
            direction = 
         = direction

#!/usr/bin/env python
# encoding: utf-8
'''
@author: JHC 
@license: None
@contact: JHC000abc@
@file: 
@time: 2022/07/19/ 16:53
@desc:
'''
from  import Qt
from  import *
from  import *
from  import *
from CDrawer import CDrawer
import sys
import cgitb
 = (1, None, 5, '')
from  import QApplication


class DrawerWidget(QWidget):

    def __init__(self, *args, **kwargs):
        super(DrawerWidget, self).__init__(*args, **kwargs)
        (Qt.WA_StyledBackground, True)
        ('DrawerWidget{background:white;}')
         = QLineEdit(self)


        layout = QVBoxLayout(self)
        ()
        (QPushButton('button', self,clicked=self._click))
        (QPushButton('button2', self, clicked=self._click2))



    def _click(self):
        if () != "":
            print("The text entered in the box is:{}".format(()))
            ()
        else:
            print("The text not entered in the box is")

    def _click2(self):
        print("Useless")





class Window(QWidget):
    def __init__(self, *args, **kwargs):
        super(Window, self).__init__(*args, **kwargs)
        (480, 960)
         = 0
         = 0

         = 0
         = 0
        # layout = QGridLayout(self)
        # (QPushButton('Sidebar', self, clicked=), 1, 0)
    def mouseMoveEvent(self, event):
        print("-----------------------mouseMoveEvent-----------------------")
        if  == 1:
             = ()
             = ()
             = 1
            if  < 100 :
                ()
        ()


    def mousePressEvent(self, event):
        if  == 1 and  == 1 and () == :
             = ()
             = ()
            print("Press",,)

        else:
            pass
        ()



    def mouseReleaseEvent(self, event):
        if  == 1 and  == 1 and () == :
             = ()
             = ()
            print("Relax", , )
        else:
            pass
        ()

    def enterEvent(self, *args, **kwargs):
        if  == 0:
             = 1
        else:
            pass

    def leaveEvent(self, *args, **kwargs):
        if  == 1:
             = 0
        else:
            pass



    def doOpenLeft(self):
        if not hasattr(self, 'leftDrawer'):
             = CDrawer(self, direction=)
            (DrawerWidget())
        ()

    def paintEvent(self, event):
        super().paintEvent(event)
         = QPainter()
        (self)
        ()
        if  != 0 and  != 0 and  == 1:
            # print(", ",,)
            ()
        else:
            ()
        ()
        ()







if __name__ == '__main__':
    app = QApplication()
    w = Window()
    ()
    (app.exec_())

This is the end of this article about PyQt5's example of the effect of smoothly moving side menu bar. For more related contents of PyQt5's smoothly moving side menu bar, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!