简体   繁体   English

类型错误:'int' 对象不支持项目分配,在线程中

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

I have the following 2 modules:我有以下两个模块:

First Keyboard.py :第一个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 :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类型错误:“int”对象不支持项目分配

试试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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