我有2个模块:
First Keyboard.py
import USB,evdev,threading,sys
global codigo
codigo = [1]
class Teclado:
def __init__(self,port):
self.puerto = USB.usb(port)
def iniciar_teclado(self):
p = threading.Thread(target=self.puerto.obtener_evento,args=(codigo))
p.start()
while 1:
if codigo[0] == evdev.ecodes.KEY_A:
print('A')
elif codigo[0] == evdev.ecodes.KEY_B:
print('B')
和USB.py:
import evdev,os,signal,sys
class usb:
def __init__(self,dev):
self.device = evdev.InputDevice(dev)
print(self.device)
def obtener_evento(self,c):
for event in self.device.read_loop():
if event.type == evdev.ecodes.EV_KEY and event.value == 1:
c[0] = event.code
因此,为了通过引用传递线程中的变量,我使用单个元素的列表。作为帮助,以下代码作为参考:
>>> c = [1]
>>> def f(list):
>>> list[0] = 'a'
>>> f(c)
>>> c[0]
'a'
但在我的代码中,在
行中c[0] = event.code
python告诉我
TypeError: 'int' object does not support item assignment
一些帮助?
答案 0 :(得分:0)
试
p = threading.Thread(target=self.puerto.obtener_evento,args=(codigo,))