如何使用通知上有按钮的python发送Windows 10通知

如何使用 python 发送支持按钮的通知,并留在操作/通知中心?

我正在尝试制作一个提醒我做事的应用程序,通知将有一个完整的和一个贪睡按钮。我尝试使用 win10toast 包,但通知没有留在操作中心,并且不支持在其上放置按钮。

通知应类似于以下内容:

enter image description here

谢谢!

stack overflow How can i can send windows 10 notifications with python that has a button on the notification
原文答案

答案:

作者头像

您可以在 winrt 中使用按钮:

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&amp;convId=01"
            activationType="background"
            hint-inputId="textBox"/>

        <action
            content="Button 1"
            arguments="action=viewdetails&amp;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))

我了解了这一点:这个 issue