0

私が2つのモジュール持っているスレッドでは、アイテムの割り当てをサポートしていません:はTypeError:「int型のオブジェクトは、

まず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

p = threading.Thread(target=self.puerto.obtener_evento,args=(codigo,)) 
を試してみてください
関連する問題