请帮我用pyqt写一个windows右下角弹窗的代码
时间: 2023-11-28 18:48:42 浏览: 30
好的,下面是一个简单的示例代码:
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt5.QtCore import Qt, QTimer
class Notification(QWidget):
def __init__(self, title, message):
super().__init__()
# 设置窗口大小和位置
self.setFixedSize(300, 100)
desktop = QApplication.desktop()
screen_rect = desktop.screenGeometry()
x = screen_rect.width() - self.width() - 20
y = screen_rect.height() - self.height() - 40
self.move(x, y)
# 设置窗口属性
self.setWindowFlags(Qt.Tool | Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
# 设置显示内容
self.title_label = QLabel(title)
self.title_label.setObjectName('title')
self.message_label = QLabel(message)
self.message_label.setObjectName('message')
layout = QVBoxLayout(self)
layout.addWidget(self.title_label)
layout.addWidget(self.message_label)
self.setLayout(layout)
# 设置样式
self.setStyleSheet('''
#title {
font-size: 18px;
font-weight: bold;
}
#message {
font-size: 16px;
}
QWidget {
background-color: #2c3e50;
color: #ecf0f1;
border-radius: 5px;
padding: 10px;
}
''')
# 显示窗口并设置自动关闭定时器
self.show()
self.timer = QTimer()
self.timer.singleShot(5000, self.close)
if __name__ == '__main__':
app = QApplication(sys.argv)
notification = Notification('通知标题', '通知内容')
sys.exit(app.exec_())
```
在上面的代码中,我们创建了一个名为 `Notification` 的类,用于显示通知。在 `__init__` 方法中,我们设置了窗口大小和位置、窗口属性、显示内容和样式,并在最后设置了自动关闭定时器。在 `main` 函数中,我们创建了一个 `Notification` 实例并运行应用程序。你可以根据自己的需求修改窗口的大小、显示内容和样式。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)