You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

开发Win10定时通知工具遇WNDPROC返回值转LRESULT类型错误

问题分析与修复方案

错误原因

  1. 循环阻塞:for i in myschedule循环内部嵌套了while True,导致第一个任务的调度循环启动后,后续任务完全无法被注册到调度器。
  2. 闭包陷阱:嵌套的functionmaker函数捕获循环变量i的引用,最终所有定时任务都会使用最后一个i的内容,而非各自对应的配置。
  3. 返回值类型不匹配:win10toast的show_toast方法在部分场景下返回None,但Windows底层消息处理要求返回整数类型,触发类型转换错误。

修复步骤

  • 将调度器的while True循环移到所有任务添加完成之后,确保所有定时任务都能被正常注册。
  • 使用默认参数绑定循环变量的当前值,规避闭包陷阱。
  • 对show_toast的返回值做强制整数转换,确保符合Windows消息处理的要求。
  • 简化输入循环的重复代码,减少冗余。

修正后的代码

import schedule
import time
from win10toast import ToastNotifier

myschedule = []

def func():
    noti = ToastNotifier()

    choice = "yes"
    while choice.lower() == "yes":
        title = input("输入通知标题: ")
        message = input("输入通知内容: ")
        tim = input("输入提醒时间(格式HH:MM): ")
        myschedule.append([title, message, tim])
        
        choice = input("是否继续添加提醒(yes/no): ")

    # 批量注册定时任务
    for item in myschedule:
        # 用默认参数绑定当前item的数值,避免闭包引用问题
        def send_notification(title=item[0], msg=item[1]):
            # 强制转换返回值为int,解决类型不匹配错误
            return int(noti.show_toast(title, msg, duration=12) or 0)
        
        schedule.every().day.at(item[2]).do(send_notification)

    # 统一启动调度循环,降低CPU占用
    while True:
        schedule.run_pending()
        time.sleep(1)

if __name__ == "__main__":
    func()

额外说明

  • 添加time.sleep(1)可以大幅降低调度循环的CPU占用率。
  • 输入处理时使用lower(),兼容大小写输入(如Yes/YES)。
  • 默认参数绑定的方式确保每个任务都能获取到对应的标题、内容和时间配置。

内容的提问来源于stack exchange,提问作者codiander

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 05:05:19