请帮我写一个pyside2 windows右下角弹窗的脚本
时间: 2023-06-06 19:08:18 浏览: 140
Windows下VS Code配置PyQt5/PySide2环境
好的,我可以帮您写一个pyside2 windows右下角弹窗的脚本,以下是代码:
```python
from PySide2.QtCore import QPoint, QTimer, Qt
from PySide2.QtGui import QColor, QPainter, QPen, QBrush, QFont
from PySide2.QtWidgets import (QApplication, QDialog, QHBoxLayout, QVBoxLayout, QLabel)
class NotificationWindow(QDialog):
def __init__(self, title, message, duration=5000):
super().__init__()
self.title = title
self.message = message
self.duration = duration
self.setWindowFlags(
Qt.FramelessWindowHint
| Qt.WindowStaysOnTopHint
| Qt.Tool
| Qt.X11BypassWindowManagerHint
)
layout = QVBoxLayout(self)
self.setLayout(layout)
self.title_label = QLabel(title, self)
self.title_label.setFont(QFont("Arial", 12))
self.message_label = QLabel(message, self)
self.message_label.setFont(QFont("Arial", 10))
layout.addWidget(self.title_label, Qt.AlignTop)
layout.addWidget(self.message_label, Qt.AlignTop)
self.timer = QTimer(singleShot=True, interval=self.duration)
self.timer.timeout.connect(self.hide)
self.setFixedSize(300, 100)
def show(self):
self.setWindowOpacity(0.7)
super().show()
self.move(QApplication.desktop().screenGeometry().right() - self.width(),
QApplication.desktop().screenGeometry().bottom() - self.height())
self.timer.start()
self.raise_()
def paintEvent(self, event):
painter = QPainter(self)
pen = QPen()
brush = QBrush(QColor(255, 255, 255, 255))
pen.setWidth(2)
pen.setColor(QColor(207, 207, 207, 255))
painter.setBrush(brush)
painter.setPen(pen)
painter.drawRoundedRect(0, 0, self.width(), self.height(), 8, 8)
app = QApplication([])
notification = NotificationWindow("Title", "Message", duration=5000)
notification.show()
app.exec_()
```
您可以在调用该类时,指定标题、消息内容和显示持续时间。示例:
```python
notification = NotificationWindow("标题", "消息内容", duration=5000)
notification.show()
```
这将在屏幕的右下角显示弹窗,显示内容为标题和消息内容。弹窗将在显示时间结束后自动消失。
阅读全文