首页 > 解决方案 > TypeError: show_toast() 在尝试发出 Windows 10 吐司通知时收到了意外的关键字参数“callback_on_click”

问题描述

我正在尝试创建一个基本程序,当我在 Windows 10 中单击 toast 通知时运行一段代码。我正在使用win10toast它,并使用“callback_on_click”方法来获得点击。

谷歌搜索了一下后,我在这里找到了这个答案

点击实现非常简单 - 只需将 callable(在这种情况下是不接收任何参数的函数)作为show_toast方法参数的值传递即可调用callback_on_click

这是我的代码:

import win10toast

def say_hello():
    toaster = win10toast.ToastNotifier()
    toaster.show_toast("Hello World!", "This is a test message from python", threaded=True, callback_on_click=say_hello)

def click_message():
    toaster = win10toast.ToastNotifier()
    print("Button clicked")
    toaster.show_toast("Hello World!", "You clicked the message! Nice!")

if __name__ == "__main__":
    say_hello()

当我运行它时,我得到:TypeError: show_toast() got an unexpected keyword argument 'callback_on_click'

我试过使用pipenv install git+https://github.com/Charnelx/Windows-10-Toast-Notifications.git#egg=win10toast,但是当我这样做时,我得到另一个错误:

ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

This is likely caused by a bug in win10toast. Report this to its maintainers.
Installation Failed

有谁知道我的问题是什么?

标签: pythonwindowstoast

解决方案


我遇到过同样的问题。这是因为该callback_on_click方法尚未合并到 PyPi 上的 win10toast 存储库中。我通过使用此命令的方法拉取分支的版本来解决这个问题。

pip install -e git+https://github.com/Charnelx/Windows-10-Toast-Notifications.git#egg=win10toast

由于 setup.py 文件,在构建过程中出现错误退出消息,但是,将复制工作的 toastNotifier 类。可以使用此命令访问模块的新版本。

from src.win10toast.win10toast import ToastNotifier

有了这个,我可以实例化一个 toastNotifier 并使用callback_on_clickCharnelx 提供的方法。


推荐阅读