PyQt5實(shí)現(xiàn)平滑移動側(cè)邊菜單欄效果的示例
CDrawer.py
#!/usr/bin/env python # encoding: utf-8 ''' @author: JHC @license: None @contact: JHC000abc@gmail.com @file: CDrawer.py @time: 2022/07/19/ 16:56 @desc: ''' from PyQt5.QtCore import Qt, QPropertyAnimation, QEasingCurve, QPoint, QPointF from PyQt5.QtGui import QMouseEvent from PyQt5.QtWidgets 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) self.setWindowFlags(self.windowFlags( ) | Qt.FramelessWindowHint | Qt.Popup | Qt.NoDropShadowWindowHint) self.setAttribute(Qt.WA_StyledBackground, True) self.setAttribute(Qt.WA_TranslucentBackground, True) # 進(jìn)入動畫 self.animIn = QPropertyAnimation( self, duration=500, easingCurve=QEasingCurve.OutCubic) self.animIn.setPropertyName(b'pos') # 離開動畫 self.animOut = QPropertyAnimation( self, duration=500, finished=self.onAnimOutEnd, easingCurve=QEasingCurve.OutCubic) self.animOut.setPropertyName(b'pos') self.animOut.setDuration(500) self.setStretch(stretch) # 占比 self.direction = direction # 方向 # 半透明背景 self.alphaWidget = QWidget( self, objectName='CDrawer_alphaWidget', styleSheet='#CDrawer_alphaWidget{background:rgba(55,55,55,100);}') self.alphaWidget.setAttribute(Qt.WA_TransparentForMouseEvents, True) self.setWidget(widget) # 子控件 def resizeEvent(self, event): self.alphaWidget.resize(self.size()) super(CDrawer, self).resizeEvent(event) def mousePressEvent(self, event): pos = event.pos() if pos.x() >= 0 and pos.y() >= 0 and self.childAt(pos) == None and self.widget: if not self.widget.geometry().contains(pos): self.animationOut() return super(CDrawer, self).mousePressEvent(event) def show(self): super(CDrawer, self).show() parent = self.parent().window() if self.parent() else self.window() if not parent or not self.widget: return # 設(shè)置Drawer大小和主窗口一致 self.setGeometry(parent.geometry()) geometry = self.geometry() self.animationIn(geometry) def animationIn(self, geometry): """進(jìn)入動畫 :param geometry: """ if self.direction == self.LEFT: # 左側(cè)抽屜 self.widget.setGeometry( 0, 0, int(geometry.width() * self.stretch), geometry.height()) self.widget.hide() self.animIn.setStartValue(QPoint(-self.widget.width(), 0)) self.animIn.setEndValue(QPoint(0, 0)) self.animIn.start() self.widget.show() elif self.direction == self.TOP: # 上方抽屜 self.widget.setGeometry( 0, 0, geometry.width(), int(geometry.height() * self.stretch)) self.widget.hide() self.animIn.setStartValue(QPoint(0, -self.widget.height())) self.animIn.setEndValue(QPoint(0, 0)) self.animIn.start() self.widget.show() elif self.direction == self.RIGHT: # 右側(cè)抽屜 width = int(geometry.width() * self.stretch) self.widget.setGeometry( geometry.width() - width, 0, width, geometry.height()) self.widget.hide() self.animIn.setStartValue(QPoint(self.width(), 0)) self.animIn.setEndValue( QPoint(self.width() - self.widget.width(), 0)) self.animIn.start() self.widget.show() elif self.direction == self.BOTTOM: # 下方抽屜 height = int(geometry.height() * self.stretch) self.widget.setGeometry( 0, geometry.height() - height, geometry.width(), height) self.widget.hide() self.animIn.setStartValue(QPoint(0, self.height())) self.animIn.setEndValue( QPoint(0, self.height() - self.widget.height())) self.animIn.start() self.widget.show() def animationOut(self): """離開動畫 """ self.animIn.stop() # 停止進(jìn)入動畫 geometry = self.widget.geometry() if self.direction == self.LEFT: # 左側(cè)抽屜 self.animOut.setStartValue(geometry.topLeft()) self.animOut.setEndValue(QPoint(-self.widget.width(), 0)) self.animOut.start() elif self.direction == self.TOP: # 上方抽屜 self.animOut.setStartValue(QPoint(0, geometry.y())) self.animOut.setEndValue(QPoint(0, -self.widget.height())) self.animOut.start() elif self.direction == self.RIGHT: # 右側(cè)抽屜 self.animOut.setStartValue(QPoint(geometry.x(), 0)) self.animOut.setEndValue(QPoint(self.width(), 0)) self.animOut.start() elif self.direction == self.BOTTOM: # 下方抽屜 self.animOut.setStartValue(QPoint(0, geometry.y())) self.animOut.setEndValue(QPoint(0, self.height())) self.animOut.start() def onAnimOutEnd(self): """離開動畫結(jié)束 """ # 模擬點(diǎn)擊外側(cè)關(guān)閉 QApplication.sendEvent(self, QMouseEvent( QMouseEvent.MouseButtonPress, QPointF(-1, -1), Qt.LeftButton, Qt.NoButton, Qt.NoModifier)) def setWidget(self, widget): """設(shè)置子控件 :param widget: """ self.widget = widget if widget: widget.setParent(self) self.animIn.setTargetObject(widget) self.animOut.setTargetObject(widget) def setEasingCurve(self, easingCurve): """設(shè)置動畫曲線 :param easingCurve: """ self.animIn.setEasingCurve(easingCurve) def getStretch(self): """獲取占比 """ return self.stretch def setStretch(self, stretch): """設(shè)置占比 :param stretch: """ self.stretch = max(0.1, min(stretch, 0.9)) def getDirection(self): """獲取方向 """ return self.direction def setDirection(self, direction): """設(shè)置方向 :param direction: """ direction = int(direction) if direction < 0 or direction > 3: direction = self.LEFT self.direction = direction
Qt.py
#!/usr/bin/env python # encoding: utf-8 ''' @author: JHC @license: None @contact: JHC000abc@gmail.com @file: QT.py @time: 2022/07/19/ 16:53 @desc: ''' from PyQt5.QtCore import Qt from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * from CDrawer import CDrawer import sys import cgitb sys.excepthook = cgitb.enable(1, None, 5, '') from PyQt5.QtWidgets import QApplication class DrawerWidget(QWidget): def __init__(self, *args, **kwargs): super(DrawerWidget, self).__init__(*args, **kwargs) self.setAttribute(Qt.WA_StyledBackground, True) self.setStyleSheet('DrawerWidget{background:white;}') self.lineedit = QLineEdit(self) layout = QVBoxLayout(self) layout.addWidget(self.lineedit) layout.addWidget(QPushButton('button', self,clicked=self._click)) layout.addWidget(QPushButton('button2', self, clicked=self._click2)) def _click(self): if self.lineedit.text() != "": print("框中輸入的文字為:{}".format(self.lineedit.text())) self.lineedit.clear() else: print("框中未輸入的文字為") def _click2(self): print("沒用") class Window(QWidget): def __init__(self, *args, **kwargs): super(Window, self).__init__(*args, **kwargs) self.resize(480, 960) self.entermouse = 0 self.flag = 0 self.x = 0 self.y = 0 # layout = QGridLayout(self) # layout.addWidget(QPushButton('側(cè)邊欄', self, clicked=self.doOpenLeft), 1, 0) def mouseMoveEvent(self, event): print("-----------------------mouseMoveEvent-----------------------") if self.entermouse == 1: self.x = event.x() self.y = event.y() self.flag = 1 if self.x < 100 : self.doOpenLeft() self.update() def mousePressEvent(self, event): if self.entermouse == 1 and self.flag == 1 and event.button() == Qt.LeftButton: self.x = event.x() self.y = event.y() print("按壓 ",self.x,self.y) else: pass self.update() def mouseReleaseEvent(self, event): if self.entermouse == 1 and self.flag == 1 and event.button() == Qt.LeftButton: self.x = event.x() self.y = event.y() print("松開 ", self.x, self.y) else: pass self.update() def enterEvent(self, *args, **kwargs): if self.entermouse == 0: self.entermouse = 1 else: pass def leaveEvent(self, *args, **kwargs): if self.entermouse == 1: self.entermouse = 0 else: pass def doOpenLeft(self): if not hasattr(self, 'leftDrawer'): self.leftDrawer = CDrawer(self, direction=CDrawer.LEFT) self.leftDrawer.setWidget(DrawerWidget(self.leftDrawer)) self.leftDrawer.show() def paintEvent(self, event): super().paintEvent(event) self.painter = QPainter() self.painter.begin(self) self.update() if self.x != 0 and self.y != 0 and self.entermouse == 1: # print("self.x,self.y ",self.x,self.y) self.update() else: self.update() self.update() self.painter.end() if __name__ == '__main__': app = QApplication(sys.argv) w = Window() w.show() sys.exit(app.exec_())
到此這篇關(guān)于PyQt5實(shí)現(xiàn)平滑移動側(cè)邊菜單欄效果的示例的文章就介紹到這了,更多相關(guān)PyQt5 平滑移動側(cè)邊菜單欄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python numpy和matlab的幾點(diǎn)差異介紹
這篇文章主要介紹了Python numpy和matlab的幾點(diǎn)差異,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07Python中Numpy與TensorFlow版本兼容問題完美解決辦法
這篇文章主要給大家介紹了關(guān)于Python中Numpy與TensorFlow版本兼容問題的完美解決辦法,確保Python版本與TensorFlow版本兼容是首要任務(wù),因?yàn)椴患嫒莸慕M合可能導(dǎo)致導(dǎo)入錯誤或其他運(yùn)行時問題,需要的朋友可以參考下2024-07-07Python報(bào)錯SyntaxError:unexpected?EOF?while?parsing的解決辦法
在運(yùn)行或編寫一個程序時常會遇到錯誤異常,這時python會給你一個錯誤提示類名,告訴出現(xiàn)了什么樣的問題,下面這篇文章主要給大家介紹了關(guān)于Python報(bào)錯SyntaxError:unexpected?EOF?while?parsing的解決辦法,需要的朋友可以參考下2022-07-07PyQt5+QtChart實(shí)現(xiàn)繪制極坐標(biāo)圖
QChart是一個QGraphicScene中可以顯示的QGraphicsWidget。本文將利用QtChart實(shí)現(xiàn)極坐標(biāo)圖的繪制,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-12-12pytest fixtures裝飾器的使用和如何控制用例的執(zhí)行順序
這篇文章主要介紹了pytest fixtures裝飾器的使用和如何控制用例的執(zhí)行順序,幫助大家更好的理解和使用pytest測試框架,感興趣的朋友可以了解下2021-01-01使用python圖形模塊turtle庫繪制櫻花、玫瑰、圣誕樹代碼實(shí)例
這篇文章主要介紹了用python繪制櫻花、玫瑰、圣誕樹代碼實(shí)例,需要的朋友可以參考下2020-03-03TensorFlow tf.nn.softmax_cross_entropy_with_logits的用法
這篇文章主要介紹了TensorFlow tf.nn.softmax_cross_entropy_with_logits的用法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04