Windows Desktop Notifications in Python

Advertisement

Advertisement

Introduction

This guide will give you two very easy ways to create desktop notifications in Windows using Python. One will use win10toast package and the other will use plyer.

This guide is aimed at Windows. For Linux notifications with libnotify, check out my tutorial Desktop Notifications in Linux with Python.

This was tested with Python 3.6 in Windows 10.

Using win10toast

The win10toast package for Python has the simplest interface, but is also limited. It's the easiest way to get started.

The source code is available on GitHub. Under the hood, win10toast uses the pywin32 package. Review the source code to see the relatively ugly underlying code that is necessary to interact with the Windows API. That's not an insult to the author of the library, just a comment about how complex the Windows API has become over time.

First, install the dependency:

python -m pip install win10toast
# python -m pip install win10toast
from win10toast import ToastNotifier

# One-time initialization
toaster = ToastNotifier()

# Show notification whenever needed
toaster.show_toast("Notification!", "Alert!", threaded=True,
                   icon_path=None, duration=3)  # 3 seconds

# To check if any notifications are active,
# use `toaster.notification_active()`
import time
while toaster.notification_active():
    time.sleep(0.1)

Using plyer

Plyer is a cross-platform library that actually works on mobile too, when used with Kivy. Plyer has way more than just notification tools. It can do screenshots, text-to-speech, vibration, GPS, bluetooth, audio, accelerometer, and more! If you want to learn more about using Kivy check out some of my streams:

First, install the plyer Python package dependency:

python -m pip install plyer

Here is some example usage:

# python -m pip install plyer
from plyer import notification

notification.notify(
    title='Here is the title',
    message='Here is the message',
    app_icon=None,  # e.g. 'C:\\icon_32x32.ico'
    timeout=10,  # seconds
)

Refer to the Official Plyer Docs for more options.

Conclusion

After this you should know how to create simple desktop notifications in Windows with custom durations and icons. These are not the only methods for creating Windows desktop notifications, but they are the easiest methods I found. Other libraries like PyQt5 offer more implementations.

Advertisement

Advertisement