首页 > 解决方案 > Python - 如何捕获 Windows 通知按钮按下事件

问题描述

问题总结

我最近使用该winrt模块成功地使用 Python 显示本机 Windows 通知。但是,我无法弄清楚如何在这些通知中检测按钮按下事件。所以我的主要问题是:如何使用 Python捕获winrt通知事件(尤其是)?ToastNotification.Activated

我已经发现或做过的事情

我在 winrt GitHub 页面上找到了一个代码片段asyncio,它使用该模块来捕获某个蓝牙事件。我尝试将其修改为使用 windows ToastNotification,但由于这是我第一次使用 asyncio 和 winrt,很遗憾我没有成功。我相信我想要捕捉的确切事件是ToastNotification.Activated此处的文档)。

以前,我也尝试过使用 和 之类的通知模块zroyawin10toast但由于这些模块非常有限,我决定尝试直接winrt方法以在布局中获得更多自由。

我的代码显示原生 Windows 通知,按钮还没有做任何事情,是

from winrt.windows.ui.notifications import ToastNotificationManager, ToastNotification
import winrt.windows.data.xml.dom as dom

# AppID is just an example here, it can be retrieved using the Powershell command "get-StartApps"
AppID = "{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\\WindowsPowerShell\\v1.0\\powershell.exe" 

nManager = ToastNotificationManager
notifier = nManager.create_toast_notifier(AppID)

tString = r"""
    <toast>
        <visual>
            <binding template='ToastGeneric'>
                <text>This is the toast title.</text>
                <text>This is the 1st line.</text>
                <text>Surprisingly, this is the 2nd line!</text>
                <text placement="attribution">This text is kinda smol :)</text>
            </binding>
        </visual>
    <actions>
        <action content="Dismiss"    arguments="action=dismiss"  />
        <action content="More info"  arguments="action=moreinfo" />
        <action content="Watch live" arguments="action=watchlive"/>
    </actions>
    </toast>
"""
xDoc = dom.XmlDocument()
xDoc.load_xml(tString)

notification = ToastNotification(xDoc)
notifier.show(notification)

我也不确定arguments="action=dismiss"etc. 做了什么,虽然我还没有详细研究这些论点,但我想它们与事件有关。如果你能在这个问题上启发我,我提前谢谢你:)

标签: pythonwindowsnotificationswindows-runtimepython-asyncio

解决方案


推荐阅读