python-win10toast

本文介绍了Python的win10toast模块,用于在Windows 10上创建和显示通知。内容包括安装、基本用法、方法介绍,如ToastNotifier对象、show_toast()、hide_toast()等,并提供了通知的相关属性和设置说明。
Python3.8

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

win10toast

调用win10toast即可触发Windows10通知,该库底层为win32api、win32con和win32gui

安装

pip install win10toast

初试

下载python.ico

import time
from win10toast import ToastNotifier

toaster = ToastNotifier()
toaster.show_toast("Hello World!!!",
                   "Python is 10 seconds awsm!",
                   icon_path="python.ico",
                   duration=10)

toaster.show_toast("Example two",
                   "This notification is in it's own thread!",
                   icon_path=None,
                   duration=5,
                   threaded=True)
# 等待线程通知完成
while toaster.notification_active():
    time.sleep(0.1)

效果
在这里插入图片描述

方法介绍

win10toast是一个用于Windows 10操作系统的Python模块,用于创建和显示Windows 10通知。以下是win10toast模块的所有方法可以用于创建、显示和管理Windows 10通知。:

  • ToastNotifier(): 创建一个ToastNotifier对象,用于发送通知。
  • show_toast(title, msg=None, duration=5, icon_path=None): 显示通知,其中title是通知的标题,msg是通知的正文,duration是通知持续时间(以秒为单位),icon_path是通知图标的路径。
  • hide_toast(): 隐藏当前正在显示的通知。
  • update_title(title): 更新当前正在显示的通知的标题。
  • update_message(msg): 更新当前正在显示的通知的正文。
  • update_duration(duration): 更新当前正在显示的通知的持续时间。
  • update_icon(icon_path): 更新当前正在显示的通知的图标。
  • exists(): 检查当前是否有正在显示的通知。
  • get_notification_duration(): 获取当前正在显示的通知的持续时间。
  • get_notification_title(): 获取当前正在显示的通知的标题。
  • get_notification_message(): 获取当前正在显示的通知的正文。
  • get_notification_icon(): 获取当前正在显示的通知的图标。

ToastNotifier通知设置

win10toast.ToastNotifier
def show_toast(self,
               title: str = "Notification",
               msg: str = "Here comes the message",
               icon_path: Any = None,
               duration: int = 5,
               threaded: bool = False) -> bool
  • title=“Notification”:str。通知标题。
  • msg=“Here comes the message”:str。通知内容。
  • icon_path=None:str or None。通知图标路径, 必须是.ico文件。
  • duration=5:int。唤起通知后持续多久再销毁通知,单位为秒。
  • threaded=False:bool。是否将该通知加入一个新线程。如果为True,该通知将加入一个新线程,程序继续往后执行,不等通知销毁。

相关属性

.wc
.hwnd
.hinst
.classAtom

notification_active

判断是否有通知处于活动状态未销毁。

# 导入
from win10toast import ToastNotifier
# 创建通知对象
TN = ToastNotifier()
# 唤起通知,并加入线程,程序继续向下执行,不等待通知销毁
TN.show_toast("通知", "这里通知一条消息", "./n8.ico", 10, threaded=True)
# 输出通知状态
print(TN.notification_active())	---> True

------

# 导入
from win10toast import ToastNotifier
# 创建通知对象
TN = ToastNotifier()
# 唤起通知,不加入线程,程序等待通知销毁后再继续执行
TN.show_toast("通知", "这里通知一条消息", "./n8.ico", 10, threaded=False)
# 输出通知状态
print(TN.notification_active())	---> False

on_destroy

.on_destroy(hwnd, msg, wparam, lparam)

销毁一个处于活跃状态的通知,一般是在其线程中活跃的通知。只需传入hwnd = TN.hwnd即可,其他参数传入None。

参考文献

  1. Windows-10-Toast-Notifications GitHub
  2. 使用 Python 制作按键触发Windows通知的脚本

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

士别三日,当挖目相待

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

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

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

打赏作者

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

抵扣说明:

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

余额充值