也许有点晚了,但是这段代码应该可以展示一个带有按钮的通知样例:
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))
app = '{1AC14E77-0...
替换为更相关的内容。 - Isiah很不幸,win10toast
欺骗并显示旧式的Windows XP通知,而不是Toast。通过XML指定Toast的内容,可以包含按钮、格式和图像等。
要显示Toast,必须使用WinRT。幸运的是,最近有人写了一个真正的答案,使用Python/WinRT包。
我不会投票关闭作为重复,因为该问题的得到赞同的答案都使用通知而不是Toast。请去点赞那个答案。
链接的答案解释了如何安装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>
一旦选择了一个操作,参数就会被发送到应用程序
pip install win10toast
from win10toast import ToastNotifier
toast = ToastNotifier()
toast.show_toast("Notification title","Notification body",duration=DURATION,icon_path="ICON PATH")
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()
import winrt.windows.ui.notifications as notifications
import winrt.windows.data.xml.dom as dom
import sys
# get python path
path = sys.executable
#create notifier
nManager = notifications.ToastNotificationManager
notifier = nManager.create_toast_notifier(path)
#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))
我从这里了解到的:这个问题。
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://..."
win10toast
,它是欺骗Toast的。Toast是由操作系统而非应用程序显示的。如果您检查win10toast的代码,您会发现它进行了一些Win32调用以显示旧式通知而非Toast。这就是为什么它无法显示任何新的UI元素。 - Panagiotis Kanavos