[英]How can i can send windows 10 notifications with python that has a button on the notification
也许为时已晚,但此代码应显示带有按钮的示例通知:
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))
不幸的是, win10toast
作弊并显示旧式的 Windows XP 通知,而不是吐司。 Toast 的内容是使用 XML 指定的,可以包含按钮、格式、图像等。
要显示祝酒词,必须使用 WinRT。 幸运的是,最近有人写了一个真正的答案,使用Python/WinRT包。
我不会投票关闭重复,因为对该问题的赞成答案都适用于通知,而不是敬酒。 请去投票那个答案。
链接的答案解释了如何使用以下命令安装 Python/WinRT:
pip install winrt
然后以惊人的少代码使用它:
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))
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>
一旦选择了一个动作,参数就会被发送到应用程序
我将使用 win10toast 模块。 第一次使用:
pip install win10toast
在cmd中安装它。
然后导入它:
from win10toast import ToastNotifier
示例通知:
toast = ToastNotifier()
toast.show_toast("Notification title","Notification body",duration=DURATION,icon_path="ICON PATH")
您可以使用https://github.com/go-toast/toast 中的 toast64.exe 应用程序。 它支持按钮、应用名称、通知持续时间和通知声音。 它很容易使用。 这比 plyer、win10toast、py-notifier、pynotify 等其他 Python 模块要好得多。
这是来自上述 Github 页面的代码片段:-
C:\Users\Example\Downloads\toast64.exe \
--app-id "Example App" \
--title "Hello World" \
--message "Lorem ipsum dolor sit amet, consectetur adipiscing elit." \
--icon "C:\Users\Example\Pictures\icon.png" \
--audio "default" --loop \
--duration "long" \
--activation-arg "https://google.com" \
--action "Open maps" --action-arg "bingmaps:?q=sushi" \
--action "Open browser" --action-arg "http://..."
您可以像这样在winrt 中使用按钮:
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(r"C:\Users\USERNAME\AppData\Local\Programs\Python\Python38\python.exe")
#define your notification as
tString = """
<toast>
<visual>
<binding template='ToastGeneric'>
<text>New notifications</text>
<text>Text</text>
<text>Second text</text>
</binding>
</visual>
<actions>
<input id="textBox" type="text" placeHolderContent="Type a reply"/>
<action
content="Send"
arguments="action=reply&convId=01"
activationType="background"
hint-inputId="textBox"/>
<action
content="Button 1"
arguments="action=viewdetails&contentId=02"
activationType="foreground"/>
</actions>
</toast>
"""
#convert notification to an XmlDocument
xDoc = dom.XmlDocument()
xDoc.load_xml(tString)
# this is not called on the main thread.
def handle_activated(sender, _):
print([sender, _])
print('Button was pressed!')
# add the activation token.
activated_token = notification.add_activated(handle_activated)
#display notification
notifier.show(notifications.ToastNotification(xDoc))
我在哪里了解到这一点:这个问题。
我没有足够的代表来评论我的其他答案的链接……但我找到了一个使用 WinRT 的解决方案,该解决方案显示可从 Python 代码中使用的按钮(与仅提供“关闭”的答案不同)选项)。 我从这里的另一个 WinRT 答案开始,但那个答案有错误并且缺少关键步骤。
我已经在此处写出了更多详细信息,但我只会为那些只需要解决方案的人提供其中的代码。 作为一个有用的示例,以下代码(Python 3.9)将使用handle_activated
函数中的子handle_activated
调用在 Windows 资源管理器窗口中打开您的“文档”文件夹。
import os,sys,time
import subprocess
import threading
import winrt.windows.ui.notifications as notifications
import winrt.windows.data.xml.dom as dom
# this is not called on the main thread!
def handle_activated(sender, _):
path = os.path.expanduser("~\Documents")
subprocess.Popen('explorer "{}"'.format(path))
def test_notification():
#define your notification as
tString = """
<toast duration="short">
<visual>
<binding template='ToastGeneric'>
<text>New notifications</text>
<text>Text</text>
<text>Second text</text>
</binding>
</visual>
<actions>
<action
content="Test Button!"
arguments=""
activationType="foreground"/>
</actions>
</toast>
"""
#convert notification to an XmlDocument
xDoc = dom.XmlDocument()
xDoc.load_xml(tString)
notification = notifications.ToastNotification(xDoc)
# add the activation token.
notification.add_activated(handle_activated)
#create notifier
nManager = notifications.ToastNotificationManager
#link it to your Python executable (or whatever you want I guess?)
notifier = nManager.create_toast_notifier(sys.executable)
#display notification
notifier.show(notification)
duration = 7 # "short" duration for Toast notifications
# We have to wait for the results from the notification
# If we don't, the program will just continue and maybe even end before a button is clicked
thread = threading.Thread(target=lambda: time.sleep(duration))
thread.start()
print("We can still do things while the notification is displayed")
if __name__=="__main__":
test_notification()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.