首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用evdev InputDevice退出python程序会导致错误

使用evdev InputDevice退出python程序会导致错误
EN

Stack Overflow用户
提问于 2020-04-01 17:15:28
回答 1查看 723关注 0票数 1

我正在使用evdev将控制器作为输入设备进行实验。当我退出程序时,我得到一条错误消息,指出delete方法(super)至少需要一个参数。我已经看过了,但我们无法找到一个解决方案来妥善处理这个问题。

程序:

代码语言:javascript
复制
# import evdev
from evdev import InputDevice, categorize, ecodes

# creates object 'gamepad' to store the data
# you can call it whatever you like
gamepad = InputDevice('/dev/input/event5')

# prints out device info at start
print(gamepad)

# evdev takes care of polling the controller in a loop
for event in gamepad.read_loop():
    # filters by event type
    if event.type == ecodes.EV_KEY and event.code == 308:
        break
    if event.type == ecodes.EV_ABS and event.code == 1:
        print(event.code, event.value)
    if event.type == ecodes.EV_ABS and event.code == 0:
        print(event.code, event.value)
    # print(categorize(event))
    if event.type == ecodes.EV_KEY:
        print(event.code, event.value)

当我使用一个特定的键时,我中断了循环,导致了这个错误消息:

代码语言:javascript
复制
Exception TypeError: TypeError('super() takes at least 1 argument (0 given)',) in <bound method InputDevice.__del__ of InputDevice('/dev/input/event5')> ignored

当我使用^C退出时,也会发生同样的情况。你有什么办法正确处理出口吗?

EN

回答 1

Stack Overflow用户

发布于 2020-10-06 23:52:39

简单地说,evdev正在等待一个事件发生,而您突然中断了这个循环。

为了正确退出执行,请删除break语句,关闭设备并结束脚本。

代码语言:javascript
复制
[...]
for event in gamepad.read_loop():
    if event.type == ecodes.EV_KEY and event.code == 308:
        gamepad.close()   #method of ..yourpythonlib/evdev/device.py
        quit()            
[...]

为了在没有错误的情况下急剧退出^C,您仍然需要使用"try-except块“关闭evdev输入设备。

代码语言:javascript
复制
for event in gamepad.read_loop():
   try:
       [...] if event [...]
   except KeyboardInterrupt:
       gamepad.close()
       raise  
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60967415

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档