简体   繁体   English

使用 python 的带有按钮的 Windows 通知

[英]Windows notification with button using python

I need to make a program that alerts me with a windows notification, and I found out that this can be simply done with the following code.我需要制作一个程序,通过 Windows 通知提醒我,我发现这可以通过以下代码简单地完成。

I don't care what library I use我不在乎我使用什么库

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

This code gives that following alert此代码给出以下警报

在此处输入图像描述

However, I want there to be a button on the notification so I can click it and it will lead me to a url.但是,我希望通知上有一个按钮,以便我可以单击它,它会引导我到一个 url。

在此处输入图像描述

Like this example.像这个例子。

Is this possible?这可能吗?

I just found this website about toast contents can anyone help me use this with python?我刚刚找到了这个关于toast 内容的网站,任何人都可以帮我在 python 中使用它吗?

This type of behavior is not supported in the currently released version of Windows-10-Toast-Notifications .当前发布的Windows-10-Toast-Notifications版本不支持此类行为。 However, a contributor created a pull request that adds functionality for a callback_on_click parameter that will call a function when the notification is clicked.但是, 贡献者创建了一个拉取请求,该请求为callback_on_click参数添加了功能,该参数将在单击通知时调用一个函数。

This has yet to be merged into the master branch, and given how long it's been since the library has been updated, I wouldn't count on it happening anytime soon.这还没有被合并到主分支中,而且考虑到库​​已经更新了多长时间,我不会指望它很快就会发生。 However, you can still install this modified version of the library to make use of this feature:但是,您仍然可以安装此修改后的库版本以使用此功能:

  • First, you'll need to uninstall the current version of win10toast from your environment (eg, pip uninstall win10toast ).首先,您需要从您的环境中卸载当前版本的win10toast (例如pip uninstall win10toast )。
  • Next, you'll need to install the modified version (eg, pip install git+https://github.com/Charnelx/Windows-10-Toast-Notifications.git#egg=win10toast ).接下来,您需要安装修改后的版本(例如, pip install git+https://github.com/Charnelx/Windows-10-Toast-Notifications.git#egg=win10toast )。

Then, you can create a toast like this:然后,您可以像这样创建吐司:

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

A complete working example:一个完整的工作示例:

from win10toast import Toast

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

When you click on the notification, you should see "Clicked!"当您单击通知时,您应该会看到“已单击!” appear in the Python console.出现在 Python 控制台中。

Important : This will only work if you're using the modified version of the library I mentioned above.重要提示:这仅在您使用我上面提到的库的修改版本时才有效。 Otherwise you will get the error: TypeError: show_toast() got an unexpected keyword argument 'callback_on_click' .否则你会得到错误: TypeError: show_toast() got an unexpected keyword argument 'callback_on_click'

You should try Zroya .你应该试试Zroya

Example:例子:

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)

Ouput Example输出示例

You can read more here: https://malja.github.io/zroya/index.html你可以在这里阅读更多: https ://malja.github.io/zroya/index.html

Sorry for not posting this sooner.很抱歉没有早点发布这个。

Here is what I have found.这是我发现的。

For installing the library:安装库:

pip install winotify

The code that you are looking for:您正在寻找的代码:

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://stackoverflow.com")

toast.show()

"FullPath.ico" is the full path of the file to put an icon to the notification. “FullPath.ico”是将图标放入通知的文件的完整路径。

To do this you can press "Ctrl + Shift + Right Click On Mouse" on the icon file and click on "Copy as path".为此,您可以在图标文件上按“Ctrl + Shift + 鼠标右键单击”,然后单击“复制为路径”。 Then paste it into the double quotation marks instead of "FullPath.ico".然后将其粘贴到双引号中,而不是“FullPath.ico”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM