python - 侦听器回调中未处理的异常
问题描述
我正在开发一个截图工具,我试图创建一个循环,每次点击都会创建名为 e1、e2、e3 和 e4 的变量,但我不知道这个循环会在一次点击后自行重启 4 次:
def on_click(x,y,button,pressed):
e4 = None
while e4 == None:
z=1
print("Assigned the edge where located at {0} {1} !".format(x,y))
exec("e + {0}= ({1},{2})".format(z,x,y))
z += 1
然后我定义了所需的 on_move 和 on_scroll 函数:
def on_move(x,y):
pass
def on_scroll(x,y,dx,dy):
pass
然后我使用了每个使用 pynput 的人都使用的代码:
with Listener(on_move=on_move,on_click=on_click,on_scroll=on_scroll) as listener:
listener.join()
在这些之后,我测试了代码,我得到了这些错误:
Unhandled exception in listener callback
Traceback (most recent call last):
File "C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pynput-1.6.8-py3.8.egg\pynput\_util\win32.py", line 380, in _handler
converted = self._convert(code, msg, lpdata)
File "C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pynput-1.6.8-py3.8.egg\pynput\_util\win32.py", line 395, in _convert
raise NotImplementedError()
NotImplementedError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pynput-1.6.8-py3.8.egg\pynput\_util\__init__.py", line 162, in inner
return f(self, *args, **kwargs)
File "C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pynput-1.6.8-py3.8.egg\pynput\_util\win32.py", line 384, in _handler
self._handle(code, msg, lpdata)
File "C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pynput-1.6.8-py3.8.egg\pynput\mouse\_win32.py", line 191, in _handle
self.on_click(data.pt.x, data.pt.y, button, pressed)
File "C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pynput-1.6.8-py3.8.egg\pynput\_util\__init__.py", line 78, in inner
if f(*args) is False:
File "C:\Users\admin\Desktop\python\sstool.py", line 13, in on_click
exec("e + {0}= ({1},{2})".format(z,x,y))
File "<string>", line 1
SyntaxError: cannot assign to operator
Traceback (most recent call last):
File "C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pynput-1.6.8-py3.8.egg\pynput\_util\win32.py", line 380, in _handler
converted = self._convert(code, msg, lpdata)
File "C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pynput-1.6.8-py3.8.egg\pynput\_util\win32.py", line 395, in _convert
raise NotImplementedError()
NotImplementedError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\admin\Desktop\python\sstool.py", line 25, in <module>
listener.join()
File "C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pynput-1.6.8-py3.8.egg\pynput\_util\__init__.py", line 210, in join
six.reraise(exc_type, exc_value, exc_traceback)
File "c:\users\admin\desktop\pynput-1.6.8\.eggs\six-1.15.0-py3.8.egg\six.py", line 702, in reraise
raise value.with_traceback(tb)
File "C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pynput-1.6.8-py3.8.egg\pynput\_util\__init__.py", line 162, in inner
return f(self, *args, **kwargs)
File "C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pynput-1.6.8-py3.8.egg\pynput\_util\win32.py", line 384, in _handler
self._handle(code, msg, lpdata)
File "C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pynput-1.6.8-py3.8.egg\pynput\mouse\_win32.py", line 191, in _handle
self.on_click(data.pt.x, data.pt.y, button, pressed)
File "C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pynput-1.6.8-py3.8.egg\pynput\_util\__init__.py", line 78, in inner
if f(*args) is False:
File "C:\Users\admin\Desktop\python\sstool.py", line 13, in on_click
exec("e + {0}= ({1},{2})".format(z,x,y))
File "<string>", line 1
SyntaxError: cannot assign to operator
如何在单击时一一分配这些变量,另一个错误的原因是什么?
解决方案
我不知道这里的正确解决方案是什么,但主要错误是因为这个声明而发生的exec("e + {0}= ({1},{2})".format(z,x,y))
。
您可以使用示例来查看相同的行为,例如 exec("e+1=(1,1)") 会引发相同的问题。
解决方案是简单地删除+
ie exec("e{0}=({1}, {2})")
- 这将按照您的预期创建 e1、e2、e3 和 e4。
推荐阅读
- discord.js - 如何从服务器中选择随机用户?
- google-analytics - 在单个请求中获取 Analytics 网络媒体资源和 GA4 媒体资源
- python - 打开时在pycharm上自动运行python脚本
- xbee - 写入通道时如何避免 TransmitException
- javascript - 如何使用 setInterval 移动 3 个图片元素
- c++ - 没有参数的 cin.get() 做什么?
- python-3.x - rust 链接因 aarch64-linux-android-clang 失败,找不到 -lpython3.6m
- powershell - 使用 Powershell 从文本文件中选择特定字符串
- sql-server - 在 SQL Server Management Studio 中创建数据库时出错
- python - 需要很好的文件解析