简体   繁体   中英

Displaying windows 10 notifications - Python

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.

After going around the internet for some time, I stumbled over this resource. It gives the option to click on the notification.

Link for resources

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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