Windows 使用 python 按钮的通知

Windows notification with button using python

我需要制作一个程序,通过 windows 通知提醒我,我发现这可以通过以下代码简单地完成。

我不在乎我使用什么库

from win10toast import ToastNotifier
toast = ToastNotifier()
toast.show_toast("alert","text")

此代码给出以下警报

但是,我希望通知上有一个按钮,这样我就可以单击它,它将引导我进入 url。

喜欢这个例子。

这可能吗?

我刚找到这个关于 toast contents 的网站 任何人都可以帮助我将它与 python 一起使用吗?

当前发布的 Windows-10-Toast-Notifications. However, a contributor created a pull request 版本不支持此类行为,该版本添加了 callback_on_click 参数的功能,该参数将在单击通知时调用函数。

这还没有合并到 master 分支中,考虑到库已经更新了多长时间,我不指望它会很快发生。但是,您仍然可以安装此库的修改版本以使用此功能:

  • 首先,您需要从环境中卸载 win10toast 的当前版本(例如 pip uninstall win10toast)。
  • 接下来,您需要安装 modified version(例如 pip install git+https://github.com/Charnelx/Windows-10-Toast-Notifications.git#egg=win10toast)。

然后,您可以像这样创建吐司:

toast.show_toast(title="Notification", msg="Hello, there!", callback_on_click=your_callback_function)

一个完整的工作示例:

from win10toast import Toast

toast = ToastNotifier()
toast.show_toast(title="Notification", msg="Hello, there!", callback_on_click=lambda: print("Clicked!"))

当您点击通知时,您应该会看到“已点击!”出现在 Python 控制台中。

重要提示:这仅在您使用我上面提到的库的修改版本时才有效。否则你会得到错误:TypeError: show_toast() got an unexpected keyword argument 'callback_on_click'.

你应该试试 Zroya.

示例:

import zroya

status = zroya.init(
    app_name="NotifyBot",
    company_name="MyBotCorp",
    product_name="NoBo",
    sub_product="core",
    version="v01"
)

if not status:
    print("Initialization failed")


# zroya is imported and initialized
template = zroya.Template(zroya.TemplateType.ImageAndText4)
#Adds text:
template.setFirstLine("Example notification")
#Adds the button
template.addAction("Ok")

zroya.show(template)

输出 Example

您可以在这里阅读更多内容:https://malja.github.io/zroya/index.html

抱歉没有早点发布。

这是我的发现。

用于安装库:

pip install winotify

您要查找的代码:

from winotify import Notification, audio

toast = Notification(app_id = "Notification",
                     title = "Alert",
                     msg = "Text",
                     duration = "long",
                     icon = r"FullPath.ico"
                     )

toast.set_audio(audio.Mail, loop=False)

toast.add_actions(label="URL Button", launch = "https://whosebug.com")

toast.show()

"FullPath.ico" 是将图标放入通知的文件的完整路径。

为此,您可以在图标文件上按“Ctrl + Shift + 鼠标右键”,然后单击“复制为路径”。然后将其粘贴到双引号中,而不是“FullPath.ico”。