python-win11toast

本文介绍了如何使用Python的win11toast库创建Windows 11通知,包括设置标题、正文、图标、声音、回调函数和自定义行为。需要注意的是,图标和图片路径必须为绝对路径,并且win11toast库支持播放音乐、显示进度条、添加按钮等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注意!!!

win11toast有个很坑的设定,icon和image等参数大部分都是需要绝对路径,相对路径有问题。尤其是windows下

win11toast

安装

pip install win11toast

用法说明

发现win11toast.toastwin11toast.notify用法一致但是toast会阻塞主线程,notify则不会。

方法

在Python中,可以使用win11toast库来生成Toast通知。这个库提供了许多方法来生成不同类型的Toast通知,下面列出了一些常用的方法及其参数:

参数

win11toast.toast 是一个用于生成 Windows 11 通知的 Python 库。它支持多种参数,包括:

  • title:通知的标题
  • body:通知的正文内容
  • icon:通知的图标,可以是图片文件路径或图标资源 ID
  • smallicon:小图标,可以是图片文件路径或图标资源 ID
  • sound:通知的声音,可以是音频文件路径或资源 ID
  • priority:通知的优先级,可以是 low、medium、high 或 urgent
  • timeout:通知的超时时间,以毫秒为单位
  • launch_url:通知点击后跳转的 URL
  • launch_target:通知点击后跳转的目标窗口或进程 ID
  • launch_params:通知点击后跳转的参数,以字典形式传递
  • clear_after_launch:通知点击后是否清除,默认为 True
  • body_template:自定义通知正文内容的模板字符串,支持变量替换
  • smallicon_template:自定义小图标内容的模板字符串,支持变量替换

这些参数可以根据具体需求进行组合使用,以生成符合要求的通知。

没有参数

from win11toast import toast

toast()

image

基本用法

image-20221013190950295

只有文字

from win11toast import toast

toast('Hello Python🐍')

image-20221229003324491

Body和on_click

on_click点击链接,进入一个网站

from win11toast import toast

toast('Hello Python', 'Click to open url', on_click='https://github.com/GitHub30/win11toast')

image

显示一大段话

from win11toast import toast

toast('Hello', 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Earum accusantium porro numquam aspernatur voluptates cum, odio in, animi nihil cupiditate molestias laborum. Consequatur exercitationem modi vitae. In voluptates quia obcaecati!')

image-20221229003442065

在Click上运行Python脚本

from win11toast import toast

toast('你好啊', '点击运行京东签到脚本', on_click=r'D:\Pycharm\自动化\我的app\京东自动签到.pyw')

由于执行脚本时的当前目录是C:\Windows\system32,因此相应地使用os. chdir()。

e.g. handler.pyw

在Windows上,您可以使用pythonw. exe可执行文件在后台运行Python脚本,该脚本将在没有可见进程或与之交互的方式的情况下运行您的程序。

Callback回调

from win11toast import toast

toast('Hello Python', 'Click to open url', on_click=lambda args: print('clicked!', args))
# clicked! {'arguments': 'http:', 'user_input': {}}

Icon自定义图标

自定义的话有两种:

图标icon ico 免费下载 - 爱给网 (aigei.com),下载到本地然后改下icon的地址

icon参数要用完整路径不能使用相对路径,同时不支持ico格式显示,目前发现png/jpg都可以

from win11toast import toast

toast('Hello', 'Hello from Python', icon=r'C:\Users\Hi\AppData\Roaming\JetBrains\PyCharm2023.2\scratches\image1.png')

image-20221229003540955

image-20231201202549324

自定义ico为方的,src要求同样为完整路径

from win11toast import toast

icon = {
    'src': r'C:\Users\Hi\AppData\Roaming\JetBrains\PyCharm2023.2\scratches\image1.png',
    'placement': 'appLogoOverride'
}

toast('Hello', 'Hello from Python', icon=icon)

image-20231201202757728

展示图片

图片在下(默认)

image同样要求完整路径

from win11toast import toast

toast('Hello', 'Hello from Python', image=r'C:\Users\Hi\AppData\Roaming\JetBrains\PyCharm2023.2\scratches\image1.png')

image-20221229003624561

Hero图片在上

from win11toast import toast

image = {
    'src': r'C:\Users\Hi\AppData\Roaming\JetBrains\PyCharm2023.2\scratches\image1.png',
    'placement': 'hero'
}

toast('Hello', 'Hello from Python', image=image)

image-20221229003646678

显示进度条

from time import sleep
from win11toast import notify, update_progress

notify(progress={
    'title': 'YouTube',
    'status': 'Downloading...',
    'value': '0',
    'valueStringOverride': '0/15 videos'
})

for i in range(1, 15+1):
    sleep(1)
    update_progress({'value': i/15, 'valueStringOverride': f'{i}/15 videos'})

update_progress({'status': 'Completed!'})

image-20221229003738058

自定义播放,播放音乐,mp3文件

from win11toast import toast

toast('Hello', 'Hello from Python', audio='ms-winsoundevent:Notification.Looping.Alarm')

文件最好是下载到本地的文件

循环播放:

src后放文件地址

from win11toast import toast

toast('Hello', 'Hello from Python', audio={'src': 'ms-winsoundevent:Notification.Looping.Alarm', 'loop': 'true'})

From URL

from win11toast import toast

toast('Hello', 'Hello from Python', audio='https://nyanpass.com/nyanpass.mp3')

From file

from win11toast import toast

toast('Hello', 'Hello from Python', audio=r"C:\Users\Admin\Downloads\nyanpass.mp3")

我不知道如何添加自定义音频,请帮助。

Loop

from win11toast import toast

toast('Hello', 'Hello from Python', audio={'loop': 'true'})
from win11toast import toast

toast('Hello', 'Hello from Python', audio={'src': 'ms-winsoundevent:Notification.Looping.Alarm', 'loop': 'true'})

Silent保持静音

from win11toast import toast

toast('Hello Python🐍', audio={'silent': 'true'})

Speak自定义播放

from win11toast import toast

toast('Hello Python🐍', dialogue='我是你爹')

文字识别

识别文字

image-20221013201041387

from win11toast import toast

toast(ocr=r'C:\Users\Hi\AppData\Roaming\JetBrains\PyCharm2023.2\scratches\image2.png')

image-20221229003927886

放大识别文字

from win11toast import toast

toast(ocr={'lang': 'ja', 'ocr': r'C:\Users\Admin\Downloads\hello.png'})

image-20221229004021895

duration增加桌面通知的持续时间

duration貌似只有"long"一个写法

from win11toast import toast

toast('Hello Python🐍', duration='long')

没有超时 + 响铃

from win11toast import toast

toast('Hello Python🐍', scenario='incomingCall')

你的吐司用于的场景,比如警报、提醒、来电或紧急情况。

Button添加按钮

点击打开Google,单个按钮,多个按钮

这个只能打开某个网页,文件夹,播放某个音乐,视频(好像)

在arguments后面进行修改,可以是网页地址,文件夹

from win11toast import toast

toast('Hello', 'Hello from Python', button={'activationType': 'protocol', 'arguments': 'https://google.com', 'content': 'Open Google'})

image-20221229004114823

from win11toast import toast

toast('Hello', 'Hello from Python', button='Dismiss')
# {'arguments': 'http:Dismiss', 'user_input': {}}

image-20231201204614003

from win11toast import toast

toast('Hello', 'Click a button', buttons=['Approve', 'Dismiss', 'Other'])

image

from win11toast import toast

buttons = [
    {'activationType': 'protocol', 'arguments': 'C:\Windows\Media\Alarm01.wav', 'content': 'Play'},
    {'activationType': 'protocol', 'arguments': 'file:///C:/Windows/Media', 'content': 'Open Folder'}
]

toast('Music Player', 'Download Finished', buttons=buttons)


image-20221229004129272

播放音乐或打开资源管理器

from win11toast import toast

buttons = [
    {'activationType': 'protocol', 'arguments': 'C:\Windows\Media\Alarm01.wav', 'content': 'Play'},
    {'activationType': 'protocol', 'arguments': 'file:///C:/Windows/Media', 'content': 'Open Folder'}
]

toast('Music Player', 'Download Finished', buttons=buttons)

image

Input输入框

from win11toast import toast

toast('Hello', 'Type anything', input='reply', button='Send')
# {'arguments': 'http:Send', 'user_input': {'reply': 'Hi there'}}

image

from win11toast import toast

toast('Hello', 'Type anything', input='reply', button={'activationType': 'protocol', 'arguments': 'http:', 'content': 'Send', 'hint-inputId': 'reply'})
# {'arguments': 'http:', 'user_input': {'reply': 'Hi there'}}

image

下拉挑选框

from win11toast import toast

toast('Hello', 'Which do you like?', selection=['Apple', 'Banana', 'Grape'], button='Submit')
# {'arguments': 'dismiss', 'user_input': {'selection': 'Grape'}}

image-20221013203314302

image-20221013203323418

非阻塞

from win11toast import notify

notify('Hello Python', 'Click to open url', on_click='https://www.python.org')

异步

from win11toast import toast_async

async def main():
    await toast_async('Hello Python', 'Click to open url', on_click='https://www.python.org')

Debug

from win11toast import toast

xml = """
<toast launch="action=openThread&amp;threadId=92187">

    <visual>
        <binding template="ToastGeneric">
            <text hint-maxLines="1">Jill Bender</text>
            <text>Check out where we camped last weekend! It was incredible, wish you could have come on the backpacking trip!</text>
            <image placement="appLogoOverride" hint-crop="circle" src="https://unsplash.it/64?image=1027"/>
            <image placement="hero" src="https://unsplash.it/360/180?image=1043"/>
        </binding>
    </visual>

    <actions>

        <input id="textBox" type="text" placeHolderContent="reply"/>

        <action
          content="Send"
          imageUri="Assets/Icons/send.png"
          hint-inputId="textBox"
          activationType="background"
          arguments="action=reply&amp;threadId=92187"/>

    </actions>

</toast>"""

toast(xml=xml)

image

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

士别三日,当挖目相待

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值