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

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)

  • 嘿,很抱歉打扰,但您似乎是互联网上唯一知道该应用程序 ID 如何工作的人,您能向我解释一下吗?我想用更相关的内容替换 `app = '{1AC14E77-0...` (2认同)
  • @Haisi 您可以通过以管理员身份启动 PowerShell 并运行以下命令“get-StartApps”来获取任何 Windows 应用程序的应用程序用户模型 ID (AUMID)。此命令将获取所有 AUMID。如果您想获取特定应用程序(例如 Windows PowerShell)的 AUMID,请运行以下命令:`get-StartApps Windows PowerShell` (2认同)

Pan*_*vos 5

不幸的是,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&amp;contentId=351"
            activationType="foreground"/>

        <action
            content="Remind me later"
            arguments="action=remindlater&amp;contentId=351"
            activationType="background"/>

    </actions>
Run Code Online (Sandbox Code Playgroud)

选择操作后,参数将发送到应用程序


pro*_*365 4

我将使用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)