简体   繁体   English

使用 python 的交互式通知窗口 10

[英]Interactive notification windows 10 using python

I wanted to know if it was possible to show notification with windows 10, that does a specific action when pressed.我想知道是否可以在 Windows 10 中显示通知,按下时会执行特定操作。 I know about plyer and win10toast but in both of those module I don't see a way to configure it to work with clicks.我知道plyerwin10toast但在这两个模块中,我都没有看到将其配置为使用点击的方法。 I wanted it so that when i press it, a link will be opened or some function will be run, to work with tkinter .我想要它,以便当我按下它时,将打开一个链接或将运行某些功能,以与tkinter Questions here on SO about this are either unanswered or incompatible now.这里关于 SO 的问题现在要么没有答案,要么不兼容。 Do you guys have any idea?你们有什么想法吗?

Snippet 1:片段 1:

from win10toast import ToastNotifier

noti = ToastNotifier()
noti.show_toast('Random','Hello World',icon_path='ico.ico',duration=60)

Also, despite giving duration=60 it either way closes automatically after approx.此外,尽管给出duration=60 ,但它会在大约之后自动关闭。 5 seconds. 5秒。

Snippet 2:片段 2:

from plyer.utils import platform
from plyer import notification

notification.notify(
    title='Here is the title',
    message='Here is the message',
    app_name='Here is the application name',
    app_icon='path/to/the/icon.' + ('ico' if platform == 'win' else 'png')
)

After some research i found win10toast works with win32gui , and some configuration with it could make it work.经过一些研究,我发现win10toast可以与win32gui一起win32gui ,并且使用它进行一些配置可以使其工作。 But i'm not sure what.但我不确定是什么。

Thanks in advance提前致谢

You can use callback_on_click method to perform a callback on clicking the notification.您可以使用callback_on_click方法在点击通知时执行回调。

As mentioned in this thread callback_on_click installation you would need to install pip install -e git+https://github.com/Charnelx/Windows-10-Toast-Notifications.git#egg=win10toast as callback_on_click is not yet merged with wintoast .正如此线程callback_on_click 安装中所述,您需要安装pip install -e git+https://github.com/Charnelx/Windows-10-Toast-Notifications.git#egg=win10toast因为callback_on_click尚未与wintoast合并。

You will get an error but your new version of toastNotifier will be copied with the existing one.您将收到错误消息,但您的新版本toastNotifier将与现有版本一起复制。

You would have to import from src.win10toast.win10toast import ToastNotifier instead of from win10toast import ToastNotifier inorder to access the callback_on_click method.您必须导入from src.win10toast.win10toast import ToastNotifier而不是from win10toast import ToastNotifier才能访问callback_on_click方法。

Please check the snippet.请检查代码段。

from src.win10toast.win10toast import ToastNotifier
import tkinter as tk
from tkinter import *
import webbrowser

top = tk.Tk()
top.geometry('50x50')
top.title('shop')

url = 'http://www.google.com'

def notify():
   noti.show_toast('Random','Hello World',duration=60, threaded=True,callback_on_click=action) 

def action():
    webbrowser.open_new(url)
    print("Done")
    
noti = ToastNotifier() 
btn = Button(top, text= "Enter", command=notify)
btn.pack()

top.mainloop()

NOTE- Make sure you use threaded=True in show_toast() so that the rest of your program will be allowed to execute while the toast is still active.注意 - 确保在show_toast()使用threaded=True以便在 toast 仍然处于活动状态时允许程序的其余部分执行。 Else your gui would freeze.否则你的 gui 会冻结。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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