Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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

enter image description here

However, I want there to be a button on the notification so I can click it and it will lead me to a url.

enter image description here

Like this example.

Is this possible?

I just found this website about toast contents can anyone help me use this with python?

like image 344
Andy_ye Avatar asked Aug 31 '20 09:08

Andy_ye


2 Answers

This type of behavior is not supported in the currently released version of 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.

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 (e.g., pip uninstall win10toast).
  • Next, you'll need to install the modified version (e.g., 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.

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'.

like image 104
JoshG Avatar answered Sep 16 '22 16:09

JoshG


You should try 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

Sorry for not posting this sooner.

like image 24
IdkWhatToCallMe Avatar answered Sep 20 '22 16:09

IdkWhatToCallMe