Python 调用系统通知
mnsd

系统通知适合用在脚本执行完成、下载结束、定时任务提醒等场景。不同系统的实现方式不一样,Python 可以直接调用第三方库,也可以调用系统命令。

Windows 10 实现

安装 win10toast

1
pip install win10toast

示例代码:

1
2
3
4
from win10toast import ToastNotifier

toast = ToastNotifier()
toast.show_toast(title="This is a title", msg="This is a message", icon_path=None, duration=10, threaded=True)

Linux 实现

通过系统自带的命令notify-send发送通知

1
notify-send -a app-name -i icon-path title content

通过第三方包调用系统通知,如dunst

1
dunstify --action="replyAction,reply" "Message received"

通过Python调用os模块执行命令

1
2
3
import os

os.system(command)

也可以用 subprocess,比直接拼接字符串更安全:

1
2
3
import subprocess

subprocess.run(["notify-send", "任务完成", "脚本已经执行结束"])

使用建议

如果只是自己电脑上用,直接调用 notify-senddunstify 最简单。如果要写成跨平台脚本,可以根据 platform.system() 判断当前系统,再分别调用不同的通知方式。

 评论
评论插件加载失败
正在加载评论插件