I\'d like to know how to create a system tray popup message with python. I have seen those in lots of softaware, but yet difficult to find resources to do it easily with any
Windows
There's now an official way to achieve that using Python/Winrt, the github explains how to map UWP API to python ones.
By following the official UWP documentation I've managed to display a small notification that also appears in Windows notification center :
import winrt.windows.ui.notifications as notifications
import winrt.windows.data.xml.dom as dom
#create notifier
nManager = notifications.ToastNotificationManager
notifier = nManager.create_toast_notifier();
#define your notification as string
tString = """
Sample toast
Sample content
"""
#convert notification to an XmlDocument
xDoc = dom.XmlDocument()
xDoc.load_xml(tString)
#display notification
notifier.show(notifications.ToastNotification(xDoc))
The setup is limited to the installation of the library
pip install winrt
Requirements
Windows 10, October 2018 Update or later
Python for Windows, version 3.7 or later
pip, version 19 or later
Bonus macOS
I've also found a way to do it in macOS by using AppleScript, the goal of the following code is to build an AppleScript code that will be executed via python os.system
import os
def displayNotification(message,title=None,subtitle=None,soundname=None):
"""
Display an OSX notification with message title an subtitle
sounds are located in /System/Library/Sounds or ~/Library/Sounds
"""
titlePart = ''
if(not title is None):
titlePart = 'with title "{0}"'.format(title)
subtitlePart = ''
if(not subtitle is None):
subtitlePart = 'subtitle "{0}"'.format(subtitle)
soundnamePart = ''
if(not soundname is None):
soundnamePart = 'sound name "{0}"'.format(soundname)
appleScriptNotification = 'display notification "{0}" {1} {2} {3}'.format(message,titlePart,subtitlePart,soundnamePart)
os.system("osascript -e '{0}'".format(appleScriptNotification))
Use asis :
displayNotification("message","title","subtitle","Pop")
Final notes
I've sum up all the previous code in two gists
Windows
macOS