Windows notification with button using python
Asked Answered
R

4

7

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?

Reparative answered 31/8, 2020 at 9:48 Comment(2)
You have opted to use a library that has gone silent 3 years ago. You're on your own now. Luckily, someone already did this for you: Quickstart: Sending a toast notification from the desktop. You can use Python/WinRT for easy access to the Windows Runtime types from Python.Groping
I mean, I don't care what library I use, I just want it to workReparative
N
10

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

Nervous answered 31/8, 2020 at 10:17 Comment(5)
Thanks! but can I please ask why I get this error ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output. when I try to install the modified version from pip install git+https://github.com/Charnelx/Windows-10-Toast-Notifications.git#egg=win10toast?Reparative
I don't know anything about your environment, so it's tough to say. Was there anything else in the log output that might provide a hint? One thing you could try is updating your setuptools (pip install --upgrade setuptools), but without knowing more about the cause of the error, I can't really offer any other suggestions. Here is the full thread that discusses this feature: github.com/jithurjacob/Windows-10-Toast-Notifications/pull/38Nervous
Thanks! I sort of just bypassed this by using pipenvReparative
That's okay too, it just depends on your environment. Glad it helped!Nervous
Despite its name, the win10toast module doesn't even support true Windows 10 Action Center Notifications. Notably, it doesn't allow you to display a custom UI (say, with a button, as explicitly asked for). It just re-purposes the decades-old Shell interface. And it's doubtful that the implementation's receiver will even live long enough to observe the single supported user interaction: A mouse click, mind you, anywhere, not just over a, say, button.Groping
A
4

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.

To do this you can press "Ctrl + Shift + Right Click On Mouse" on the icon file and click on "Copy as path". Then paste it into the double quotation marks instead of "FullPath.ico".

Arva answered 11/5, 2022 at 12:1 Comment(0)
F
1

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.

Faena answered 20/7, 2021 at 15:36 Comment(0)
E
0

Updating the accepted answer:

Libraries like plyer, winotify and Windows 10 Toast Notification are either not being updated or lack functionalities like callback on click. Also, they do not support having multiple buttons like dismiss/clear toast, reply, etc.

I found win11toast library which supports callback function and has many more advanced features than the earlier mentioned libraries.

Here's a simple example with on-click callback:

from win11toast import toast

toast('Hello Python', 'Click to open url', on_click=lambda args: your_function(args))

Another example which includes multiple buttons as well:

from win11toast import toast

toast('Hello', 'Click a button', buttons=['Approve', 'Dismiss', 'Other'])

This would show a toast like:

Image Showing Toast

Echinoderm answered 27/11, 2023 at 20:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.