摘要:涧花然暮雨,潭树暖春云。 —岑森-

组件描述

一个气泡弹窗,出现在鼠标点击位置,用于装饰
使用工具: Pyside2

主要代码

class BubbleLabel(QLabel):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.SubWindow)
        self.setAttribute(Qt.WA_TranslucentBackground)

        self.setText("功德+1")
        self.setStyleSheet("""
        QLabel {
                font-family: "STCaiyun";
                font-size: 16pt;
                width: 250px;
                border-radius: 20px;
                margin: 5px;
            }""")
        self.animation = QPropertyAnimation(self, b"pos")
        self.animation.setDuration(1000)  # 动画持续时间1秒
        self.animation.setEasingCurve(QEasingCurve.OutQuad)
        self.animation.finished.connect(self.deleteLater)  # 动画结束时删除标签


def animate_bubble(bubble, start_x, end_y):
    # 设置动画起始和结束位置
    bubble.animation.setStartValue(QPoint(start_x, bubble.y() - 50))
    bubble.animation.setEndValue(QPoint(start_x, end_y))

    # 开始动画
    bubble.animation.start()

使用方式

    # 示例用法
    def __init__(self):
        super().__init__()
        self.setWindowTitle("气泡动画效果")
        self.resize(800, 600)

    def mousePressEvent(self, event):
        # 获取鼠标点击位置
        pos = event.globalPos() - self.pos()

        # 创建气泡标签
        bubble = BubbleLabel(self)
        bubble.move(pos)
        bubble.show()

        # 设置动画目标位置(向上移动50像素)
        target_y = pos.y() - 100
        animate_bubble(bubble, pos.x(), target_y)

成果展示

成果展示