在自己寫了玩的代碼的時候想在某些特定情況觸發提示(後臺自動運行的情況下),可以搜到的大部分是右下角提示之類比較醜的自定義提示,而其實現在win10已經有自己的通知欄,如果能用自帶的通知欄絕對是一個很優秀的選擇。搜了很久後發現了一個庫叫做win10toast。
這個庫最後是18年更新的,感覺是不會有更新了,但只要不會出問題就好,開發者github:jithurjacob/Windows-10-Toast-Notifications
安裝
用pip安裝:
pip install win10toast
這個需要同時安裝pywin32,沒有安裝的話pip會自動一起安裝的。
示例
from win10toast import ToastNotifier
toaster = ToastNotifier()
# 有icon的版本
toaster.show_toast("Hello World!!!",
"Python is 10 seconds awsm!",
icon_path="custom.ico",
duration=10)
# 無icon,採用python的icon,且採用自己的線程
toaster.show_toast("Example two",
"This notification is in it's own thread!",
icon_path=None,
duration=5,
threaded=True)
# 等待提示框關閉
while toaster.notification_active(): time.sleep(0.1)
提示框截圖:
本來想要一個可以永久長時間需要手動關閉的提示框,可惜似乎開發者並沒有支持,經過嘗試採用-1是有效的,但是會報錯。