当我尝试使用 winrt 发出通知时,出现错误
When i try to make a notification with winrt, it makes an error
我正在尝试让 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))
然而,它 returns 这个错误,当我尝试 运行 它时。
notifier = notifications.ToastNotificationManager.create_toast_notifier()
RuntimeError: Element not found.
我的系统满足 winrt
的要求ui
Windows 10, October 2018 Update or later.
Python for Windows, version 3.7 or later
pip, version 19 or later
我该如何解决这个错误?我不能使用其他模块,因为 winrt 是 olny 模块(据我所知),您可以在其中创建 ui 按钮等通知元素。
出现异常是因为您需要在create_toast_notifier()
中提供applicationID
例如 create_toast_notifier("MyApplicationId")
.
这也描述了 但在 c# 中。
我刚刚解决了关于消除错误但未显示通知的相同问题。
此 Microsoft doc 建议“重要提示:每次调用 CreateToastNotifier 时,您都必须在开始屏幕上包含应用程序快捷方式的 AppUserModelID。如果不这样做,您的 Toast 将不会显示。”。 =13=]
然后我按照我找到的说明 here 找到了 AppUserModelID,它原来是我的 Python 可执行文件的完整路径。
例如:
notifier = nManager.create_toast_notifier("C:\...\Programs\Python\Python38\python.exe")
这个方法在 Python 3.9.1
和 pip 21.0.1
上对我有用
打开 PowerShell 输入以下命令 get-StartApps
它 returns Name
和 AppID
参见 table 或 imgur 上的图片以供参考。
Name AppID
---- -----
Calculator Microsoft.WindowsCalculator_8wekyb3d8bbwe!App
这很方便
get-StartApps | Where-Object {$_.Name -like '*Application Name*'}
get-StartApps | Where-Object {$_.Name -like '*Python*'}
Copy/Paste把AppID
改成create_toast_notifier("Microsoft.WindowsCalculator_8wekyb3d8bbwe!App")
示例:
#create notifier
nManager = notifications.ToastNotificationManager
notifier = nManager.create_toast_notifier("Microsoft.WindowsCalculator_8wekyb3d8bbwe!App");
我正在尝试让 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))
然而,它 returns 这个错误,当我尝试 运行 它时。
notifier = notifications.ToastNotificationManager.create_toast_notifier()
RuntimeError: Element not found.
我的系统满足 winrt
的要求uiWindows 10, October 2018 Update or later.
Python for Windows, version 3.7 or later
pip, version 19 or later
我该如何解决这个错误?我不能使用其他模块,因为 winrt 是 olny 模块(据我所知),您可以在其中创建 ui 按钮等通知元素。
出现异常是因为您需要在create_toast_notifier()
中提供applicationID
例如 create_toast_notifier("MyApplicationId")
.
这也描述了
我刚刚解决了关于消除错误但未显示通知的相同问题。
此 Microsoft doc 建议“重要提示:每次调用 CreateToastNotifier 时,您都必须在开始屏幕上包含应用程序快捷方式的 AppUserModelID。如果不这样做,您的 Toast 将不会显示。”。 =13=]
然后我按照我找到的说明 here 找到了 AppUserModelID,它原来是我的 Python 可执行文件的完整路径。
例如:
notifier = nManager.create_toast_notifier("C:\...\Programs\Python\Python38\python.exe")
这个方法在 Python 3.9.1
和 pip 21.0.1
上对我有用
打开 PowerShell 输入以下命令 get-StartApps
它 returns Name
和 AppID
参见 table 或 imgur 上的图片以供参考。
Name AppID ---- ----- Calculator Microsoft.WindowsCalculator_8wekyb3d8bbwe!App
这很方便
get-StartApps | Where-Object {$_.Name -like '*Application Name*'}
get-StartApps | Where-Object {$_.Name -like '*Python*'}
Copy/Paste把AppID
改成create_toast_notifier("Microsoft.WindowsCalculator_8wekyb3d8bbwe!App")
示例:
#create notifier
nManager = notifications.ToastNotificationManager
notifier = nManager.create_toast_notifier("Microsoft.WindowsCalculator_8wekyb3d8bbwe!App");