Reputation: 15098
I wanted to know if it was possible to show notification with windows 10, that does a specific action when pressed. 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. I wanted it so that when i press it, a link will be opened or some function will be run, to work with tkinter
. Questions here on SO about this are either unanswered or incompatible now. Do you guys have any idea?
Snippet 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. 5 seconds.
Snippet 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. But i'm not sure what.
Thanks in advance
Upvotes: 1
Views: 2267
Reputation: 2431
You can use callback_on_click
method to perform a callback on clicking the notification.
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
.
You will get an error but your new version of toastNotifier
will be copied with the existing one.
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.
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. Else your gui would freeze.
Upvotes: 1