使用 Python 建立 Windows 10 Toast 通知
我們可以使用Python建立一個用於顯示 Windows 系統事件的通知程式。使用win10toast模組可以輕鬆實現。如果你熟悉Android中的Toast,那麼理解Python中的Toast通知就易如反掌。我們可以為發生的每個事件生成通知作為提醒。讓我們看看。
在命令列中執行以下命令來安裝win10toast模組:
pip install win10toast
如果模組成功安裝,執行命令後將得到以下結果。
Collecting win10toast Downloading https://files.pythonhosted.org/packages/d4/ba/95c0ea87d9bcad68b90d8cb130a313b939c88d8338a2fed7c11eaee972fe/win10toast-0.9-py2.py3-none-any.whl Collecting pypiwin32 (from win10toast) Downloading https://files.pythonhosted.org/packages/d0/1b/2f292bbd742e369a100c91faa0483172cd91a1a422a6692055ac920946c5/pypiwin32-223-py3-none-any.whl Requirement already satisfied: setuptools in c:\users\hafeezulkareem\anaconda3\lib\site-packages (from win10toast) (40.8.0) Requirement already satisfied: pywin32>=223 in c:\users\hafeezulkareem\anaconda3\lib\site-packages (from pypiwin32->win10toast) (223) Installing collected packages: pypiwin32, win10toast Successfully installed pypiwin32-223 win10toast-0.9
建立 Toast 通知步驟
從 win10toast 中匯入 ToastNotifier 類。
例項化該類。
呼叫 show_toast('標題', '訊息', duration = 時間(秒), icon_path = '.ico 檔案路徑') 方法,並傳入所需引數。
如果成功,通知持續時間結束後,將得到 True 作為輸出。
讓我們來看一個簡單的例子。
示例
## program to generate a simple toast notifier
from win10toast import ToastNotifier
## instantiating the class
notifier = ToastNotifier()
## invoking the show_toast() method with required arguments notifier.show_toast("Sample Notification", "You are learning at Tutorialspoint", duration = 25, icon_path = "globe.ico")執行上述程式後,你將得到以下結果。

你可以在系統事件發生時新增此通知程式。如果你對本教程有任何疑問,請在評論區提出。
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP