0

Short: How do I make windows 10 notifications with a button or just making the notification clickable? ( using python)

Long: I want to make notifications in windows 10, and I want to be able to click on the notification when it pops up. When the notification is then clicked, it should execute a function. I have looked around a lot of 'solutions, but none of them seems to work. The best one I have gotten by far is from the 'Windows-10-Toast-Notifications' (win10toast), but with a modification by Charnelx. To install his code, I tried to use the following:

pip install git+https://github.com/Charnelx/Windows-10-Toast-Notifications.git#egg=win10toast

It doesn't seem to work when I try to install it though. I'm using python 3.8.9.

I hope someone can help me.

1 Answer 1

1

You can use this python module called winrt.

#importing required modules
import winrt.windows.ui.notifications as notifications
import winrt.windows.data.xml.dom as dom
from time import sleep

# create notification objects
nManager = notifications.ToastNotificationManager
notifier = nManager.create_toast_notifier(r"C:\Users\USERNAME\AppData\Local\Programs\Python\Python38\python.exe")

# PUT YOUR USERNAME INSTEAD OF USERNAME  
# put your python path there.

# define the xml notification document.
tString = """
<toast>
    <visual>
        <binding template='ToastGeneric'>
            <text>Another Message from Tim!</text>
            <text>Hi there!</text>
        </binding>
    </visual>

    <actions>
        <action
           content="View Message"
           arguments="test1"
           activationType="backround"/>
    </actions>
</toast>
"""

# load the xml document.
xDoc = dom.XmlDocument()
xDoc.load_xml(tString)

notification = notifications.ToastNotification(xDoc)

# display notification
notifier.show(notification)

# this is not called on the main thread.
def handle_activated(sender, _):
    print([sender, _])
    print('Button was pressed!')

# add the activation token.
activated_token = notification.add_activated(handle_activated)

# do something else on the main thread.
while True:
    sleep(1)

Where I learnt about this: this issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.