目录
  • 前言
  • 实现步骤
  • 补充
    • 使用与场景

前言

win10toast是一个windows通知的出发框架,使用它可以轻松的调起系统通知。通过它可以很方便的做一个定时通知的功能应用。

实现步骤

安装调起通知的依赖库

pip install win10toast

导入相关的第三方依赖库

from win10toast import toastnotifier  # 导入系统通知对象
import time  # 系统时间模块
import datetime
from threading import timer  # 定时器

初始化通知调用对象

notify = toastnotifier()  # 初始化系统通知对象

初始化windows通知相关的参数,设置定时通知间隔时间等。

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定时器来定时的调起应用发送通知。

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()

主函数入口调用。

if __name__ == '__main__':
    show_toast()

补充

win10 没有提供简单命令行方式来触发桌面通知,所以只能使用python来写通知脚本。

一番搜索,找到 win10toast 。但这开源仓库已无人维护,通过 github fork 的关系图,找到 win10toast-click,一个正在更新功能更全的 python 第三方模块。

使用与场景

qbittorrent 支持下载完成后运行外部程序,由于 qbittorrent 没有下载完成后桌面通知的功能,所以我们通过python脚本来完成。

通过 win10toast-click 仓库中示例,就可以完成一个简单的通知脚本。

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 运行外部程序

python <脚本路径>/qb_win_toast.py --title "下载完成" --message "%n 下载完成"

到此这篇关于python调用win10toast框架实现定时调起系统通知的文章就介绍到这了,更多相关python win10toast调起系统通知内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!