我正在使用win10toast,但由于它是Win32,我尝试使用WinRT 实现它。我用一个文件模块做到了。

WindowsのPythonでデスクトップ通知(トースト)作ってみた【WinRT】WindowsのPythonでデスクトップ通知(トースト)作ってみた【WinRT】

安装

pip install win11toast

用法

from win11toast import toast

toast('Hello Python?')

WindowsのPythonでデスクトップ通知(トースト)作ってみた【WinRT】

文本

from win11toast import toast

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

WindowsのPythonでデスクトップ通知(トースト)作ってみた【WinRT】

文字换行

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!')

WindowsのPythonでデスクトップ通知(トースト)作ってみた【WinRT】

点击运行 Python

from win11toast import toast

toast('Hello Pythonista', 'Click to execute python', on_click=r'C:\Users\Admin\Downloads\handler.py')
# {'arguments': 'C:\\Users\\Admin\\Downloads\\handler.py', 'user_input': {}}

由于执行脚本时当前目录为C:\Windows\system32,请相应os.chdir()
例子:半德。 py

打回来

from win11toast import toast

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

图标

from win11toast import toast

toast('Hello', 'Hello from Python', icon='https://unsplash.it/64?image=669')

WindowsのPythonでデスクトップ通知(トースト)作ってみた【WinRT】

方形图标

from win11toast import toast

icon = {
    'src': 'https://unsplash.it/64?image=669',
    'placement': 'appLogoOverride'
}

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

WindowsのPythonでデスクトップ通知(トースト)作ってみた【WinRT】

图片

from win11toast import toast

toast('Hello', 'Hello from Python', image='https://4.bp.blogspot.com/-u-uyq3FEqeY/UkJLl773BHI/AAAAAAAAYPQ/7bY05EeF1oI/s800/cooking_toaster.png')

WindowsのPythonでデスクトップ通知(トースト)作ってみた【WinRT】

英雄

from win11toast import toast

image = {
    'src': 'https://4.bp.blogspot.com/-u-uyq3FEqeY/UkJLl773BHI/AAAAAAAAYPQ/7bY05EeF1oI/s800/cooking_toaster.png',
    'placement': 'hero'
}

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

WindowsのPythonでデスクトップ通知(トースト)作ってみた【WinRT】

进度条

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!'})

WindowsのPythonでデスクトップ通知(トースト)作ってみた【WinRT】

属性
https://docs.microsoft.com/en-ca/uwp/schemas/tiles/toastschema/element-progress

音效

from win11toast import toast

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

音效列表
https://docs.microsoft.com/en-us/uwp/schemas/tiles/toastschema/element-audio

音频网址
from win11toast import toast

toast('Hello', 'Hello from Python', audio='https://nyanpass.com/nyanpass.mp3')
音频文件
from win11toast import toast

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

无限循环

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'})

安静

from win11toast import toast

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

文字转语音

from win11toast import toast

toast('Hello Python?', dialogue='Hello world')

字符识别

from win11toast import toast

toast(ocr='https://i.imgur.com/oYojrJW.png')

WindowsのPythonでデスクトップ通知(トースト)作ってみた【WinRT】

from win11toast import toast

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

WindowsのPythonでデスクトップ通知(トースト)作ってみた【WinRT】

通知长度

from win11toast import toast

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

显示 25 秒
https://docs.microsoft.com/en-us/uwp/schemas/tiles/toastschema/element-toast

按钮

from win11toast import toast

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

WindowsのPythonでデスクトップ通知(トースト)作ってみた【WinRT】

from win11toast import toast

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

WindowsのPythonでデスクトップ通知(トースト)作ってみた【WinRT】

from win11toast import toast

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

WindowsのPythonでデスクトップ通知(トースト)作ってみた【WinRT】

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

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)

WindowsのPythonでデスクトップ通知(トースト)作ってみた【WinRT】

输入对话框

from win11toast import toast

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

WindowsのPythonでデスクトップ通知(トースト)作ってみた【WinRT】

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'}}

WindowsのPythonでデスクトップ通知(トースト)作ってみた【WinRT】

选择对话框

from win11toast import toast

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

WindowsのPythonでデスクトップ通知(トースト)作ってみた【WinRT】

WindowsのPythonでデスクトップ通知(トースト)作ってみた【WinRT】

没有论据

from win11toast import toast

toast()

WindowsのPythonでデスクトップ通知(トースト)作ってみた【WinRT】

非阻塞

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

木星

from win11toast import notify

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

WindowsのPythonでデスクトップ通知(トースト)作ってみた【WinRT】

from win11toast import toast_async

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

WindowsのPythonでデスクトップ通知(トースト)作ってみた【WinRT】

import urllib.request
from pathlib import Path
src = str(Path(urllib.request.urlretrieve("https://i.imgur.com/p9dRdtP.jpg")[0]).absolute())

from win11toast import toast_async
await toast_async('にゃんぱすー', audio='https://nyanpass.com/nyanpass.mp3', image={'src': src, 'placement':'hero'})
from win11toast import toast_async

await toast_async('Hello Python?', dialogue='にゃんぱすー')

调试

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)

WindowsのPythonでデスクトップ通知(トースト)作ってみた【WinRT】

通知可视化工具您可以使用实时调试 XML
WindowsのPythonでデスクトップ通知(トースト)作ってみた【WinRT】

致谢


原创声明:本文系作者授权爱码网发表,未经许可,不得转载;

原文地址:https://www.likecs.com/show-308622945.html

相关文章:

  • 2021-06-09
  • 2021-08-17
  • 2022-01-02
  • 2022-12-23
  • 2021-06-04
  • 2021-05-15
  • 2021-04-20
  • 2021-10-11
猜你喜欢
  • 2022-12-23
  • 2022-02-02
  • 2022-02-08
  • 2021-10-14
  • 2022-01-22
  • 2022-01-21
  • 2022-12-23
相关资源
相似解决方案