文件 "/usr/lib/python3.9/enum.py",第 405 行,__getattr__从 None 引发 AttributeError(name) 是什么意思?



我已经连接了一个模拟操纵杆到我的树莓派3B,我正试图得到上下左右的运动映射到W A S d代码看起来像这样:

#!/usr/bin/env python3
import RPi.GPIO as GPIO
import ADC0834
import time
from pynput.keyboard import Key, Controller
BtnPin = 22
def setup():
# Set the GPIO modes to BCM Numbering
GPIO.setmode(GPIO.BCM)
GPIO.setup(BtnPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
ADC0834.setup()
def destroy():
# Release resource
GPIO.cleanup()
def loop():
keyboard = Controller()
while True:
x_val = ADC0834.getResult(0)
y_val = ADC0834.getResult(1)
Btn_val = GPIO.input(BtnPin)
# Map joystick input to keyboard keys
if y_val < 127:
keyboard.press(Key.W)
keyboard.release(Key.W)
elif y_val > 127:
keyboard.press(Key.S)
keyboard.release(Key.S)
if x_val < 131:
keyboard.press(Key.A)
keyboard.release(Key.A)
elif x_val > 131:
keyboard.press(Key.D)
keyboard.release(Key.D)

time.sleep(0.2)

if __name__ == '__main__':
setup()
try:
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the program destroy() will be executed.
destroy()


当代码从终端运行时,我得到这个错误消息:
Traceback (most recent call last):
File "/home/pi/Joystick.py", line 46, in <module>
loop()
File "/home/pi/Joystick.py", line 31, in loop
keyboard.press(Key.S)
File "/usr/lib/python3.9/enum.py", line 405, in __getattr__
raise AttributeError(name) from None
AttributeError: S

我已经通过sudo pip3 install pynput下载了库的输出,并且我也尝试在代码中更改了W A S D的大小写。模拟输出也可以工作。我只是不知道如何正确地映射到wasd。如有任何帮助,不胜感激。

Key对象没有像S这样的普通字母的属性。

如果你想按一个普通的字母键,使用:

keyboard.press('S')
keyboard.release('S')

相关内容

  • 没有找到相关文章

最新更新