Ale*_*der 6 python windows notifications button
如何使用 python 发送支持按钮的通知,并留在操作/通知中心?
我正在尝试制作一个提醒我做某事的应用程序,并且通知将有一个完整的和一个小睡按钮。我尝试使用win10toast包,但是通知没有留在操作中心,并且不支持在其上放置按钮。
通知应类似于以下内容:
谢谢!
小智 10
也许为时已晚,但此代码应显示带有按钮的示例通知:
import winrt.windows.ui.notifications as notifications
import winrt.windows.data.xml.dom as dom
app = '{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\\WindowsPowerShell\\v1.0\\powershell.exe'
#create notifier
nManager = notifications.ToastNotificationManager
notifier = nManager.create_toast_notifier(app)
#define your notification as string
tString = """
<toast>
<visual>
<binding template='ToastGeneric'>
<text>Sample toast</text>
<text>Sample content</text>
</binding>
</visual>
<actions>
<action
content="Delete"
arguments="action=delete"/>
<action
content="Dismiss"
arguments="action=dismiss"/>
</actions>
</toast>
"""
#convert notification to an XmlDocument
xDoc = dom.XmlDocument()
xDoc.load_xml(tString)
#display notification
notifier.show(notifications.ToastNotification(xDoc))
Run Code Online (Sandbox Code Playgroud)
不幸的是,win10toast它会作弊并显示旧式的 Windows XP 通知,而不是 toast。Toast的内容是使用 XML 指定的,可以包含按钮、格式、图像等。
要显示 toast,必须使用 WinRT。幸运的是,最近有人使用Python/WinRT包写了一个真正的答案。
我不会投票结束重复,因为对该问题的投票答案都适用于通知,而不是祝酒词。请投票赞成该答案。
链接的答案解释了如何使用以下命令安装 Python/WinRT:
pip install winrt
Run Code Online (Sandbox Code Playgroud)
然后用很少的代码来使用它:
import winrt.windows.ui.notifications as notifications
import winrt.windows.data.xml.dom as dom
#create notifier
nManager = notifications.ToastNotificationManager
notifier = nManager.create_toast_notifier();
#define your notification as string
tString = """
<toast>
<visual>
<binding template='ToastGeneric'>
<text>Sample toast</text>
<text>Sample content</text>
</binding>
</visual>
</toast>
"""
#convert notification to an XmlDocument
xDoc = dom.XmlDocument()
xDoc.load_xml(tString)
#display notification
notifier.show(notifications.ToastNotification(xDoc))
Run Code Online (Sandbox Code Playgroud)
Toast内容文章解释了如何通过代码或 XML 创建内容。按钮描述如下,例如:
<actions>
<action
content="See more details"
arguments="action=viewdetails&contentId=351"
activationType="foreground"/>
<action
content="Remind me later"
arguments="action=remindlater&contentId=351"
activationType="background"/>
</actions>
Run Code Online (Sandbox Code Playgroud)
选择操作后,参数将发送到应用程序
我将使用win10toast模块。首次使用:
pip install win10toast
Run Code Online (Sandbox Code Playgroud)
在cmd中安装它。
然后导入它:
from win10toast import ToastNotifier
Run Code Online (Sandbox Code Playgroud)
通知示例:
toast = ToastNotifier()
toast.show_toast("Notification title","Notification body",duration=DURATION,icon_path="ICON PATH")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4759 次 |
| 最近记录: |