环境配置 安装 win10toast本项目中,我们将使用 win10toast 模块向桌面发送通知。
win10toast,一个易于使用的用于唤起Windows通知的第三方库。
https://github.com/jithurjacob/Windows-10-Toast-Notificationshttps://github.com/jithurjacob/Windows-10-Toast-Notifications
将自动安装依赖库:pypiwin32
,setuptools
pip install win10toast -i https://pypi.tuna.tsinghua.edu.cn/simple
详细信息
C:\Users\ASUS>pip show win10toast
Name: win10toast
Version: 0.9
Summary: An easy-to-use Python library for displaying Windows 10 Toast Notifications
Home-page: https://github.com/jithurjacob/Windows-10-Toast-Notifications
Author: Jithu R Jacob
Author-email: jithurjacob@gmail.com
License: BSD
Location: c:\python38\lib\site-packages
Requires: pypiwin32, setuptools
Required-by:
代码文件
main.py
import time
import queue
from threading import Thread
from win10toast import ToastNotifier
QUEUE_SIZE = 20
time_queue = queue.Queue(QUEUE_SIZE) # 时间队列
toaster = ToastNotifier() # 构造通知对象
def toast(header, text, icon=None, time_min=0, duration=10, threaded=True) -> None:
"""向桌面发送通知"""
time_min *= 60
time.sleep(time_min) # 等待
toaster.show_toast(f"{header}", f"{text}", icon_path=icon, duration=duration, threaded=threaded) # 发送通知
while toaster.notification_active(): # 活动状态中
time.sleep(0.005)
def format_time(time_dict):
"""格式化时间字典"""
if time_dict["minute"] >= 60:
time_dict["hour"] += time_dict["minute"] // 60
time_dict["minute"] %= 60
return time_dict
def init_time(interval=30):
"""初始化时间"""
time_dict = {"hour": 0, "minute": 0}
time_queue.put(time_dict.copy())
for _ in range(QUEUE_SIZE - 1):
time_dict["minute"] += interval
time_dict = format_time(time_dict)
time_queue.put(time_dict.copy()) # 加入队列
def get_message(hour, minute) -> tuple:
"""获取通知"""
msg_header = "屏幕使用时间"
msg_text = "您已使用 {} 小时 {} 分钟"
suffix_text = " by zerui666"
icon_path = "Icon.ico"
header = msg_header
if hour == 0 and minute == 0:
text = "您已开始使用计算机"
elif hour < 1:
text = f"您已使用 {hour} 小时 {minute} 分钟"
elif hour < 3:
text = f"您已使用 {hour} 小时 {minute} 分钟,请注意保护眼睛 ovo"
elif hour < 5:
text = f"您已使用 {hour} 小时 {minute} 分钟,长时间用眼有害健康 ovo"
else:
text = f"您已使用 {hour} 小时 {minute} 分钟,请善待自己的眼睛 ovo"
text += suffix_text
return header, text, icon_path
def use_timer():
while not time_queue.empty():
now = time_queue.get()
hour, minute = now['hour'], now['minute']
print(f"Hour: {hour}, Minute: {minute} \nAll set! \n")
message = get_message(hour, minute)
header = message[0]
text = message[1]
icon_path = message[2]
thread = Thread(target=toast, args=(header, text, icon_path, hour * 60 + minute)) # 多线程发送通知
thread.start()
if __name__ == "__main__":
init_time()
use_timer()
Icon.ico
>">完整代码及打包代码看过来->>https://download.csdn.net/download/m0_66420734/85373465
--The End--欢迎分享,转载请注明来源:内存溢出
评论列表(0条)