在Python中显示windows 10通知

2024-10-04 03:16:32 发布

您现在位置:Python中文网/ 问答频道 /正文

简短:如何使用按钮制作windows 10通知,或仅使通知可单击?(使用python)

Long:我想在windows 10中发出通知,并且我想在通知弹出时能够单击它。然后单击通知时,它应该执行一个函数。我环顾了很多解决方案,但似乎都不管用。到目前为止,我得到的最好的一个来自“Windows-10-Toast-Notifications”(win10toast),但是经过了Charnelx的修改。 为了安装他的代码,我尝试使用以下方法:

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

但是,当我尝试安装它时,它似乎不起作用。我正在使用python 3.8.9

我希望有人能帮助我


Tags: installpip方法函数代码gitwindows解决方案
2条回答

您可以使用这个名为winrt的python模块

#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)

我从中了解到:这个issue

在网上浏览了一段时间后,我偶然发现了这个资源。它提供了单击通知的选项

Link for resources

相关问题 更多 >