前言
win10toast是一个windows通知的出发框架,使用它可以轻松的调起系统通知。通过它可以很方便的做一个定时通知的功能应用。
实现步骤
安装调起通知的依赖库
1
|
pip install win10toast |
导入相关的第三方依赖库
1
2
3
4
|
from win10toast import ToastNotifier # 导入系统通知对象 import time # 系统时间模块 import datetime from threading import Timer # 定时器 |
初始化通知调用对象
1
|
notify = ToastNotifier() # 初始化系统通知对象 |
初始化windows通知相关的参数,设置定时通知间隔时间等。
1
2
3
4
5
|
notify_head = '主人,来通知啦!' notify_min = 1.0 notify_text = '已经过了' + str ( int (notify_min)) + '分钟了,该喝水了!' notify_sen = notify_min * 1 |
通知调起时,是使用win10toast的show_toast()函数,参数和定义如下面。
- '''
- def show_toast(self, title="Notification", msg="Here comes the message",
- icon_path=None, duration=5, threaded=False):
- """Notification settings.
- :title: notification title
- :msg: notification message
- :icon_path: path to the .ico file to custom notification
- :duration: delay in seconds before notification self-destruction
- """
- '''
show_toast()函数的使用,采用timer定时器来定时的调起应用发送通知。
1
2
3
4
5
6
7
|
def show_toast(): print ( '当前时间:%s' % (datetime.datetime.now().strftime( '%Y-%m-%d %H:%M:%S' ))) notify.show_toast(f "{notify_head}" , f "{notify_text}" , duration = 5 , threaded = True , icon_path = '水杯.ico' ) while notify.notification_active(): time.sleep( 0.005 ) timer = Timer(notify_sen, show_toast) timer.start() |
主函数入口调用。
1
2
|
if __name__ = = '__main__' : show_toast() |
补充
Win10 没有提供简单命令行方式来触发桌面通知,所以只能使用Python来写通知脚本。
一番搜索,找到 win10toast 。但这开源仓库已无人维护,通过 github fork 的关系图,找到 win10toast-click,一个正在更新功能更全的 Python 第三方模块。
使用与场景
qBittorrent 支持下载完成后运行外部程序,由于 qBittorrent 没有下载完成后桌面通知的功能,所以我们通过Python脚本来完成。
通过 win10toast-click
仓库中示例,就可以完成一个简单的通知脚本。
1
2
3
4
5
6
7
8
9
10
11
12
|
import argparse from win10toast_click import ToastNotifier parser = argparse.ArgumentParser() parser.add_argument( '--title' , help = '通知标题' ) parser.add_argument( '--message' , help = '通知内容' ) args = parser.parse_args() toaster = ToastNotifier() toaster.show_toast(title = args.title, msg = args.message) |
qBittorrent 提供了一些参数可以传递给调用程序,使用 argparse
内建模块来接收。
qBittorrent 运行外部程序
1
|
python <脚本路径> / qb_win_toast.py - - title "下载完成" - - message "%N 下载完成" |
到此这篇关于Python调用win10toast框架实现定时调起系统通知的文章就介绍到这了,更多相关Python win10toast调起系统通知内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://www.cnblogs.com/lwsbc/p/15851236.html