140
164

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

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

Last updated at Posted at 2022-08-09

win10toastを使っているのですがWin32のため、WinRTで実装してみました。シングルファイルモジュールで作ってみました。

PythonPyPI

インストール

pip install win11toast

使い方

from win11toast import toast

toast('Hello Python🐍')

image

本文

from win11toast import toast

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

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

クリック時にPythonを実行

from win11toast import toast

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

スクリプト実行時のカレントディレクトリがC:\Windows\system32なので適宜os.chdir()してください。
例: handler.pyw

.pywにするとバックグラウンドで処理されウィンドウが表示されなくなります。

スクリーンショット (9).png

コールバック

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

image

四角いアイコン

from win11toast import toast

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

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

image

画像

from win11toast import toast

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

image

ヘロ

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)

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

image
image

属性
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

音声URL
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')

image

from win11toast import toast

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

image

通知の長さ

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 Python🐍', scenario='incomingCall')

alarm, reminder, incomingCall, urgentも設定可能

image.png

ボタン

from win11toast import toast

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

image

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

image

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

入力ダイアログ

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

image

引数なし

from win11toast import toast

toast()

image

ノンブロッキング

from win11toast import notify

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

Async/Await

from win11toast import toast_async

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

Jupyter

from win11toast import notify

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

image

from win11toast import toast_async

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

image

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)

image

Notifications VisualizerでXMLをリアルタイムにデバッグできます。
image

謝辞

140
164
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
140
164

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?