Reputation: 199
I am trying to build simple Python application, which will add icon to Tray. I am also adding 2 menuitems to this tray icon, one to show Windows 10 toast message, and another one to stop application.
Program is launching correctly, icon appears, menu also appears.
So, process, which I ran with self.icon.run()
should wait, untill I will not call self.icon.stop()
The problem is, when I press first menuitem ("Test Notification") - toast notification is shown correctly, but AFTER this process goes out of self.icon.run()
loop, and program finishes itself.
And I do not understand why is it so. According to documentation self.icon.run()
should wait untill stop() call.
Am I doing something incorrecly? Here is my code:
from win10toast import ToastNotifier
from pystray import *
from PIL import Image
class SysTrayUI:
def __init__(self):
self.icon_path = "icon.png"
self.menu_items = [
MenuItem("Test Notification", lambda: toaster.show_toast("Test")),
MenuItem("Exit", lambda: self.icon.stop()),
]
self.menu = Menu(*self.menu_items)
self.icon = Icon("Test Name", menu=self.menu)
self.icon.icon = Image.open(self.icon_path)
self.icon.run()
toaster = ToastNotifier()
app = SysTrayUI()
Upvotes: 1
Views: 919
Reputation: 939
This will help, Better you create a separate function and pass the message as shown below (This worked for me). It didn't work for me when I did the same without creating a function. So follow below format
def notify(msg):
n = ToastNotifier()
n.show_toast("Champzz", msg, duration= 10, threaded=True)
Upvotes: 2
Reputation: 199
Found the solution
toaster.show_toast
function has threaded
parameter, which allows to launch function in separate thread
So, if you write it like this:
MenuItem("Test Notification", lambda: toaster.show_toast("Test", threaded=True)),
Then this will not influence execution of self.icon.run()
Upvotes: 0