简体   繁体   中英

TypeError: 'int' object does not support item assignment, In threads

I have the following 2 modules:

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')

and 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

So to pass by reference a variable in a thread, i use a list of a single element. As help, the following code has been taken as reference:

>>> c = [1]
>>> def f(list):
>>>     list[0] = 'a'
>>> f(c)
>>> c[0]
'a'

but in my code, in the line

c[0] = event.code

python tell me

TypeError: 'int' object does not support item assignment

试试

p = threading.Thread(target=self.puerto.obtener_evento,args=(codigo,))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM