我知道这是一个在这里被多次询问的问题,但即使在查看并尝试使用本网站上的所有解决方案之后,也无法解决我的问题。这是我的代码:
def trackMouse():
global x, y
x = 0
y = 0
x_max = 1000
y_max = 1000
keyboardEvent = evdev.InputDevice('/dev/input/event0')
mouseEvent = evdev.InputDevice('/dev/input/event1')
async def print_events(device):
async for event in device.async_read_loop():
if event.type == ecodes.EV_REL:
if event.code == ecodes.REL_X:
print("REL_X")
x += 1
if event.code == ecodes.REL_Y:
print("REL_Y")
y += 1
if event.type == ecodes.EV_KEY:
c = categorize(event)
if c.keystate == c.key_down:
print(c.keycode)
for device in keyboardEvent, mouseEvent:
asyncio.ensure_future(print_events(device))
loop = asyncio.get_event_loop()
loop.run_forever()
运行此循环时出现的错误是:
任务异常从未检索到未来:.print_events() 完成,定义在 etho.py:113> exception=UnboundLocalError("local variable 'a' referenced before assignment",)>
Traceback(最近一次调用最后):
文件“/ usr /lib/python3.5/asyncio/tasks.py”,第 239 行,在
_step 结果 = coro.send(None)
文件“etho.py”,第 124 行,在 print_events
if x += 1:
UnboundLocalError: local variable ' x' 在赋值之前引用
无论我在哪里分配变量或声明它,当我尝试在 if 语句中使用它或添加它时,它都会引发错误,但当我将它设置为等于数字时不会。我认为这与它所处的奇怪循环有关。