“ToastNotifier”对象没有属性“classAtom”

2024-05-26 04:22:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我运行下面的代码

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

我得到以下错误:

^{pr2}$

Tags: path代码fromimporthelloworldisshow
3条回答

看来问题出在源代码本身。Code Screenshot here

如果您看到该图像,如果在self.classAtom = RegisterClass(self.wc)发生异常,则他们没有处理它,这将使classAtom变量未声明。这就是你的问题所在。所以,要修复它,只需制作classAtom = ""或其他东西。但是,这并不能解决问题。在

考虑到被接受的答案可能会破坏更多的东西,我会发布这个。在

正如inAFlash在他的评论中提到的,这是一个依赖性问题。更新setuptools将解决此问题。在

为此,只需运行:python -m pip install setuptools upgrade

正如@InAFlash提到的,classAtom没有返回任何内容,原因是已经有一个注册的通知。搞乱了源代码,我向except块添加了一个异常打印,如下所示:

try:
    self.classAtom = RegisterClass(self.wc)
except Exception as e:
    print(e)

我在线程中运行我的通知,当代码遇到这一点时,它会打印出以下错误:

^{pr2}$

我想我找到了一个解决方案,尽管它有点脱离了“垃圾邮件”通知的想法。通过向self.wc.lpszClassName变量添加title,它使它成为一个不同的通知。所以总体来说,代码应该是这样的:

self.wc = WNDCLASS()
self.hinst = self.wc.hInstance = GetModuleHandle(None)
self.wc.lpszClassName = str(f"PythonTaskbar{title}")  # must be a string
self.wc.lpfnWndProc = message_map  # could also specify a wndproc.
try:
    self.classAtom = RegisterClass(self.wc)
except Exception as e:
    print(e)

这个解决方案适用于我的场景,所以不确定这是否是最好的解决方案,但我会继续使用它。在

希望这有帮助。在

相关问题 更多 >