Windows desktop notifications in 3 lines of Python code
Why do we need a notification?
There could be many cases in which you need to popup a notification.
Suppose you have written a script for emailing a couple thousand people. And as you know it may take a while to finish. You can’t just wait it out. So to solve this problem, why not popup a notification when it is finished while you enjoy your favourite movie.
Showing the Notification
To popup a notification, what can we do?
Well, the simplest answer would be to use a GUI framework or module to popup something like a notification. But for that we will have to write a lot of code.
Why not use a module that’s already written?
Well, it turns out that there is already a module for notifications in windows. It is called ‘win10toast’.
But first we have to install it. To install it, just run the following commands in the terminal -
$ pip install pypiwin32
$ pip install win10toast
Once you are done, now let’s write the code -
from win10toast import ToastNotifier #for importing ToastNotifier object toaster = ToastNotifier() #creating the object
toaster.show_toast("Python!" , #popping the notification "This is working!", duration=10)
For showing the notification asynchronously (without freezing the script) -
from win10toast import ToastNotifier #for importing ToastNotifier object toaster = ToastNotifier() #creating the object
toaster.show_toast("Python!" , #popping the notification "This is working!", duration=10, threaded=True)
Win10toast documentation - https://pypi.org/project/win10toast/
For more helpful tutorials - https://www.youtube.com/channel/UC3VAed5huwZHOaE3q-Ap8qg?view_as=subscriber















