注意!!!
win11toast有个很坑的设定,icon和image等参数大部分都是需要绝对路径,相对路径有问题。尤其是windows下
win11toast
安装
pip install win11toast
用法说明
发现win11toast.toast
和win11toast.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()
基本用法
只有文字
from win11toast import toast
toast('Hello Python🐍')
Body和on_click
on_click点击链接,进入一个网站
from win11toast import toast
toast('Hello Python', 'Click to open url', on_click='https://github.com/GitHub30/win11toast')
显示一大段话
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!')
在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')
自定义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同样要求完整路径
from win11toast import toast
toast('Hello', 'Hello from Python', image=r'C:\Users\Hi\AppData\Roaming\JetBrains\PyCharm2023.2\scratches\image1.png')
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)
显示进度条
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!'})
自定义播放,播放音乐,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='我是你爹')
文字识别
识别文字
from win11toast import toast
toast(ocr=r'C:\Users\Hi\AppData\Roaming\JetBrains\PyCharm2023.2\scratches\image2.png')
放大识别文字
from win11toast import toast
toast(ocr={'lang': 'ja', 'ocr': r'C:\Users\Admin\Downloads\hello.png'})
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'})
from win11toast import toast
toast('Hello', 'Hello from Python', button='Dismiss')
# {'arguments': 'http:Dismiss', 'user_input': {}}
from win11toast import toast
toast('Hello', 'Click a button', buttons=['Approve', 'Dismiss', 'Other'])
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)
播放音乐或打开资源管理器
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)
Input输入框
from win11toast import toast
toast('Hello', 'Type anything', input='reply', button='Send')
# {'arguments': 'http:Send', 'user_input': {'reply': 'Hi there'}}
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'}}
下拉挑选框
from win11toast import toast
toast('Hello', 'Which do you like?', selection=['Apple', 'Banana', 'Grape'], button='Submit')
# {'arguments': 'dismiss', 'user_input': {'selection': 'Grape'}}
非阻塞
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&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&threadId=92187"/>
</actions>
</toast>"""
toast(xml=xml)