如何从 python winrt toast 获取按钮和文本输入?

Posted

技术标签:

【中文标题】如何从 python winrt toast 获取按钮和文本输入?【英文标题】:How to get buttons and text input from a python winrt toast? 【发布时间】:2021-08-07 19:49:43 【问题描述】:

如何在按下按钮时获取代码以调用函数,并从 pywinrt toast 的文本框中获取输入?

我正在制作一个 python 库,它可以让 windows toast 通知更容易在 python 中制作。

代码:

import winrt.windows.ui.notifications as notifications
import winrt.windows.ui.notifications.management as listener
import winrt.windows.data.xml.dom as dom

#create notifier
nManager = notifications.ToastNotificationManager
notifier = nManager.create_toast_notifier(r"C:\Users\Admin\AppData\Local\Programs\Python\Python38\python.exe")

#define your notification as
tString = """
<toast>
    <visual>
        <binding template='ToastGeneric'>
            <text>Hi!</text>
            <text>I am a toast.</text>
        </binding>
    </visual>
    <actions>
        <input id="textBox" type="text" placeHolderContent="Type a reply"/>
        <action
            content="send message"
            arguments="action=reply&amp;convId=01"
            activationType="background"
            hint-inputId="textBox"/>
        <action
            content="OK"
            arguments="action=viewdetails&amp;contentId=02"
            activationType="foreground"/>
    </actions>
</toast>
"""

#convert notification to an XmlDocument
xDoc = dom.XmlDocument()
xDoc.load_xml(tString)

#display notification
notifier.show(notifications.ToastNotification(xDoc))

谢谢!

【问题讨论】:

【参考方案1】:

按钮可以这样使用:

#importing required modules
import winrt.windows.ui.notifications as notifications
import winrt.windows.data.xml.dom as dom
from time import sleep
import sys

# get python path
path = sys.executable

# create notification objects
nManager = notifications.ToastNotificationManager
notifier = nManager.create_toast_notifier(path)

# define the xml notification document.
tString = """
<toast>
    <visual>
        <binding template='ToastGeneric'>
            <text>Another Message from Tim!</text>
            <text>Hi there!</text>
        </binding>
    </visual>

    <actions>
        <action
           content="View Message"
           arguments="test1"
           activationType="backround"/>
    </actions>
</toast>
"""

# load the xml document.
xDoc = dom.XmlDocument()
xDoc.load_xml(tString)

notification = notifications.ToastNotification(xDoc)

# display notification
notifier.show(notification)

# 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)

# do something else on the main thread.
while True:
    sleep(1)

我是从哪里了解到这个的:这个issue。 还回答了:here。

【讨论】:

但是文本输入呢?

以上是关于如何从 python winrt toast 获取按钮和文本输入?的主要内容,如果未能解决你的问题,请参考以下文章

如何从应用程序外部获取我的 Toast 通知?

如何在 WinRT 中获取文件大小?

如何从自定义列表视图中获取选定项目并在 toast 消息中打印?

python 怎样获取toast?

Python+Appium 获取 toast 方法的封装

python appium 封装获取toast方法