一、安装win10sdk库(python3.10)
二、编写如下代码
import winsdk.windows.ui.notifications as notifications
from winsdk.windows.data.xml.dom import XmlDocument, XmlNodeList
def notifier(message) -> None:
notif_manager = notifications.ToastNotificationManager
toast_xml: XmlDocument = notif_manager.get_template_content(
notifications.ToastTemplateType.TOAST_TEXT02)
toast_text_elements: XmlNodeList = toast_xml.get_elements_by_tag_name(
"text")
toast_text_elements[0].append_child(
toast_xml.create_text_node("title"))
toast_text_elements[1].append_child(toast_xml.create_text_node(message))
toast = notifications.ToastNotification(toast_xml)
notif_manager.create_toast_notifier("Python").show(toast)
调用
notifier("hello world")
结果:


好处:可以轻松用pyinstaller打包,并且通知不处理会留在通知栏,而win10toast疑似必须是控制台程序才能触发通知,且通知不停留
本文介绍了如何在Python中不依赖win10toast库来实现Windows10通知栏的通知功能。通过安装win10sdk库,编写简单代码,可以实现通知的创建,而且这种通知会保留在通知栏,即使程序被关闭。相比win10toast,这种方法更适合用pyinstaller打包,且在非控制台程序中也能触发通知。

454

被折叠的 条评论
为什么被折叠?



