使用 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