PyQt 右下角提示(Toast Notification)示例

在 PyQt 中实现右下角的提示通知(通常称为 Toast Notification)是一个常见需求。这样的通知通常用于显示应用程序的状态更新或其他重要信息。

准备工作

确保你的环境中已经安装了 Python 和 PyQt5。

bash 复制代码
pip install PyQt5

示例代码

下面是一个简单的示例,展示如何使用 QSystemTrayIconQMessageBox 来实现右下角的提示通知。

python 复制代码
from PyQt5.QtWidgets import QApplication, QSystemTrayIcon, QMenu, QAction, QMessageBox
from PyQt5.QtGui import QIcon

def show_notification():
    tray.showMessage("Notification", "This is a sample notification!", QIcon("icon.png"), 2000)

app = QApplication([])

# 创建系统托盘图标
tray = QSystemTrayIcon(QIcon("icon.png"), parent=app)

# 创建右键菜单
menu = QMenu()
show_action = QAction("Show Notification")
show_action.triggered.connect(show_notification)
menu.addAction(show_action)
exit_action = QAction("Exit")
exit_action.triggered.connect(app.quit)
menu.addAction(exit_action)

tray.setContextMenu(menu)
tray.show()

# 显示一个初始通知
show_notification()

app.exec_()

代码解释

  • QSystemTrayIcon 是用于在系统托盘区域显示图标的类。
  • QMenuQAction 用于创建托盘图标的右键菜单。
  • show_notification 函数用于显示通知。它使用 QSystemTrayIconshowMessage 方法。

运行示例

保存上面的代码到一个文件(例如 tray_notification.py),并运行它。你应该会看到系统托盘区域有一个新的图标,右键点击它会显示一个菜单,其中有一个 "Show Notification" 选项。点击这个选项,你会看到一个右下角的提示通知。

这样,你就成功地使用 PyQt 实现了一个简单的右下角提示通知功能。你可以根据需要进一步定制这个功能。

相关推荐
爸爸61914 小时前
UIAbility 生命周期在日记 App 中的实践
后端·华为·harmonyos·鸿蒙系统
大阳光男孩1 天前
Spring Boot 整合 Debezium 实现 MySQL 增量数据监听(嵌入式版)
spring boot·后端·mysql
皮皮林5511 天前
ThreadLocal 不香了?ScopedValue才是王道?
后端
X-⃢_⃢-X1 天前
一、第一阶段:认识 Spring Boot
java·spring boot·后端
前端工作日常1 天前
我学习到的Java程序的生命周期
java·后端
AskHarries1 天前
GitHub 登录配置流程
后端
Sinclair1 天前
安企CMS的安装-常见安装问题排查
运维·后端
Sinclair1 天前
安企CMS的安装-安装引导与初始化
运维·后端
Sinclair1 天前
安企CMS的安装-源码编译安装
运维·后端·go
Sinclair1 天前
安企CMS的安装-同一服务器多站点部署
运维·后端