一、效果图
二、示例代码
from PySide6.QtWidgets import QApplication, QWidget from PySide6.QtCore import Qt, Slot, QPoint, QPropertyAnimation, QEasingCurve from PySide6.QtGui import QColorfrom views.login_ui import Ui_LoginForm import sysclass Login(QWidget, Ui_LoginForm):drag_pos = QPoint()animation = Nonedef __init__(self):super().__init__()self.setWindowFlags(Qt.WindowType.FramelessWindowHint)self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)self.setupUi(self)@Slot()def on_pushButton_close_clicked(self):self.close()def paintEvent(self, event):if not self.animation:self.animation = QPropertyAnimation(self)self.animation.setTargetObject(self)self.animation.setPropertyName(b"windowOpacity")# 设置初动画始值 self.animation.setStartValue(0)# 设置动画结束值self.animation.setEndValue(1)# 设置动画线性 self.animation.setEasingCurve(QEasingCurve.Linear)# 设置动画时长self.animation.setDuration(500)# 设置循环次数 如果是-1则无限循环self.animation.setLoopCount(1)self.animation.start()event.accept()def mousePressEvent(self, event):if event.buttons() == Qt.MouseButton.LeftButton:self.drag_pos = event.globalPos() - self.frameGeometry().topLeft()event.accept()def mouseMoveEvent(self, event):if event.buttons() == Qt.MouseButton.LeftButton:self.move(event.globalPos() - self.drag_pos)event.accept()if __name__ == '__main__':app = QApplication(sys.argv)d = Login()d.show()sys.exit(app.exec())