Python函数工作正常,除非导入



我试图将其提炼为要点。此代码运行良好。

from evdev import InputDevice, ecodes
dev = InputDevice('/dev/input/event2')   # this is the joystick event file
button = {304: 'A', 305: 'B', 307: 'X', 308: 'Y' }  # Xbox360 button mappings
exit = False
def joystick(dev):
try:
for event in list(dev.read()):
if button[event.code] == 'A' and event.value == 1:
print('Button A pressed.')
if button[event.code] == 'X' and event.value == 1:
print('Button X pressed.')
return True
except: pass
return False

while exit == False:
exit = joystick(dev)

然而,如果我将操纵杆函数移到一个单独的文件中(包括正确的evdev导入(并导入它,代码就会中断(函数"操纵杆"将始终返回false,无法识别操纵杆事件(。

有什么想法吗?我知道软件/硬件接口可能很棘手,但这太荒谬了。

发现错误。当我把joystick函数移到文件之外时,我忽略了用它来移植button字典。try语句掩盖了我通常会通过省略字典而得到的错误。

最新更新