Pythonを使ってOS通知を作成する方法はいくつかあります。ここでは、主にWindowsとmacOSで動作する2つの方法を紹介します。
Windowsでの通知
Windowsでは、win10toast
やplyer
などのライブラリを使用してデスクトップ通知を作成することができます。以下にwin10toast
を使用した例を示します。
from win10toast import ToastNotifier
toast = ToastNotifier()
toast.show_toast("Notification", "Notification body", duration=20, icon_path="icon.ico", threaded=True)
また、plyer
を使用した例も以下に示します。
from plyer import notification
notification.notify(
title="Sample Notification",
message="This is a sample notification",
timeout=10
)
macOSでの通知
macOSでは、AppleScriptを使用して通知を作成することができます。以下にその例を示します。
import os
def displayNotification(message, title=None, subtitle=None, soundname=None):
os.system("""
osascript -e 'display notification "{}" with title "{}"'
""".format(message, title))
これらのコードを使用することで、PythonからOSレベルの通知を作成することが可能になります。具体的な使用例や詳細な説明は各ライブラリの公式ドキュメンテーションを参照してください。