| 26 | |
| 27 | |
| 28 | class BubbleLabel(QWidget): |
| 29 | BackgroundColor = QColor(195, 195, 195) |
| 30 | BorderColor = QColor(150, 150, 150) |
| 31 | |
| 32 | def __init__(self, *args, **kwargs): |
| 33 | text = kwargs.pop("text", "") |
| 34 | super(BubbleLabel, self).__init__(*args, **kwargs) |
| 35 | # 设置无边框置顶 |
| 36 | self.setWindowFlags( |
| 37 | Qt.Window | Qt.Tool | Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.X11BypassWindowManagerHint) |
| 38 | # 设置最小宽度和高度 |
| 39 | self.setMinimumWidth(200) |
| 40 | self.setMinimumHeight(48) |
| 41 | self.setAttribute(Qt.WA_TranslucentBackground, True) |
| 42 | layout = QVBoxLayout(self) |
| 43 | # 左上右下的边距(下方16是因为包括了三角形) |
| 44 | layout.setContentsMargins(8, 8, 8, 16) |
| 45 | self.label = QLabel(self) |
| 46 | layout.addWidget(self.label) |
| 47 | self.setText(text) |
| 48 | # 获取屏幕高宽 |
| 49 | self._desktop = QApplication.instance().desktop() |
| 50 | |
| 51 | def setText(self, text): |
| 52 | self.label.setText(text) |
| 53 | |
| 54 | def text(self): |
| 55 | return self.label.text() |
| 56 | |
| 57 | def stop(self): |
| 58 | self.hide() |
| 59 | self.animationGroup.stop() |
| 60 | self.close() |
| 61 | |
| 62 | def show(self): |
| 63 | super(BubbleLabel, self).show() |
| 64 | # 窗口开始位置 |
| 65 | startPos = QPoint( |
| 66 | self._desktop.screenGeometry().width() - self.width() - 100, |
| 67 | self._desktop.availableGeometry().height() - self.height()) |
| 68 | endPos = QPoint( |
| 69 | self._desktop.screenGeometry().width() - self.width() - 100, |
| 70 | self._desktop.availableGeometry().height() - self.height() * 3 - 5) |
| 71 | print(startPos, endPos) |
| 72 | self.move(startPos) |
| 73 | # 初始化动画 |
| 74 | self.initAnimation(startPos, endPos) |
| 75 | |
| 76 | def initAnimation(self, startPos, endPos): |
| 77 | # 透明度动画 |
| 78 | opacityAnimation = QPropertyAnimation(self, b"opacity") |
| 79 | opacityAnimation.setStartValue(1.0) |
| 80 | opacityAnimation.setEndValue(0.0) |
| 81 | # 设置动画曲线 |
| 82 | opacityAnimation.setEasingCurve(QEasingCurve.InQuad) |
| 83 | opacityAnimation.setDuration(4000) # 在4秒的时间内完成 |
| 84 | # 往上移动动画 |
| 85 | moveAnimation = QPropertyAnimation(self, b"pos") |