首页 > 解决方案 > 侦听器回调中未处理的异常

问题描述

我正在开发一个截图工具,我试图创建一个循环,每次点击都会创建名为 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

如何在单击时一一分配这些变量,另一个错误的原因是什么?

标签: pythonpython-3.xpyautoguipynput

解决方案


我不知道这里的正确解决方案是什么,但主要错误是因为这个声明而发生的exec("e + {0}= ({1},{2})".format(z,x,y))

您可以使用示例来查看相同的行为,例如 exec("e+1=(1,1)") 会引发相同的问题。

解决方案是简单地删除+ie exec("e{0}=({1}, {2})")- 这将按照您的预期创建 e1、e2、e3 和 e4。


推荐阅读