Python 通知

桌面通知

安装 Python 库

pip install windows_toasts

代码实现示例

桌面通知

from windows_toasts import WindowsToaster, Toast
from time import sleep

toaster = WindowsToaster("Python")  # 创建一个名为Python的Toast通知
newToast = Toast()  # 创建一个新的Toast通知
newToast.text_fields = ["Hello, world!"]  # 设置Toast通知的文本字段
newToast.on_activated = lambda _: print("Toast clicked!")  # 设置Toast通知的激活事件
toaster.show_toast(newToast)  # 显示Toast通知

sleep(5)  # 等待5秒,以便查看Toast通知

无进度 Toast 通知

from windows_toasts import InteractableWindowsToaster, Toast, ToastProgressBar

toaster = InteractableWindowsToaster(
    "Windows-Toasts"
)  # 创建一个名为Windows-Toasts的Toast通知

# 创建一个ToastProgressBar对象,用于显示进度条
progressBar = ToastProgressBar(
    "正在准备...",
    "Python 4 release",
    progress=None,
    progress_override="? millenniums remaining",
)

newToast = Toast(text_fields=["123"], progress_bar=progressBar)

toaster.show_toast(newToast)
from windows_toasts import InteractableWindowsToaster, Toast, ToastProgressBar
from time import sleep
from threading import Thread
from queue import Queue

def notices(q):
    toaster = InteractableWindowsToaster(
        "Py 打包工具"
    )  # 创建一个名为Py 打包工具的Toast通知

    # 创建一个ToastProgressBar对象,用于显示进度条
    progressBar = ToastProgressBar(
        "正在打包...",
        progress=None,
        progress_override="剩余 4 步骤",
    )

    newToast = Toast(text_fields=[""], progress_bar=progressBar)
    newToast.text_fields = ["123"]

    toaster.show_toast(newToast)

    while True:
        item = q.get()
        if item is None:
            break
        progressBar.progress_override = f"剩余 {6-item} 步骤"
        toaster.update_toast(newToast)
        q.task_done()
        sleep(2)

    toaster.clear_toasts()  # 清除所有Toast通知

    newToast = Toast(text_fields=["打包成功!"])
    toaster.show_toast(newToast)

def msg():
    a = [1, 2, 3, 4, 5]
    for i in a:
        q.put(i)
        print("put", i)

q = Queue()
thread = Thread(target=notices, args=(q,))
thread.start()
msg()
q.join()
q.put(None)

thread.join()

有进度 Toast 通知

from windows_toasts import InteractableWindowsToaster, Toast, ToastProgressBar
from time import sleep

toaster = InteractableWindowsToaster("进度条")

progressBar = ToastProgressBar(
    status="正在下载...",
    caption="Python 4 release",
)

newToast = Toast(text_fields=["正在准备程序"], progress_bar=progressBar)

toaster.show_toast(newToast)

for i in range(101):
    progressBar.progress = i / 100
    progressBar.progress_override = f"{i}%"
    toaster.update_toast(newToast)
    sleep(0.1)

progressBar.status = "正在解压..."

for i in range(101):
    progressBar.progress = i / 100
    progressBar.progress_override = f"{i}%"
    toaster.update_toast(newToast)
    sleep(0.01)

progressBar.status = "完成"
progressBar.progress = 1
progressBar.progress_override = "100%"
toaster.update_toast(newToast)

通知推送

使用 Bark 进行通知推送。

代码实现示例

# 使用 Bark 推送通知
from requests import post

URL = "http(s)://<server>/<key>"

data = {"title": "标题", "body": "内容:\n123", "group": "测试"}
headers = {"Content-Type": "application/json", "charset": "utf-8"}

post(URL, json=data, headers=headers)

requests 需要安装:pip install requests