【问题标题】:TypeError: 'int' object does not support item assignment, In threadsTypeError:“int”对象不支持项目分配,在线程中
【发布时间】:2016-09-29 23:41:35
【问题描述】:

我有以下 2 个模块:

第一个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

蟒蛇告诉我

TypeError: 'int' 对象不支持项目分配

【问题讨论】:

    标签: python pass-by-reference python-multithreading input-devices


    【解决方案1】:

    试试

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

    【讨论】:

    • 对于任何将来观看此内容的人:不同之处在于 args=(codigo) 不是元组,而是被评估为 int。如果括号中有逗号,则它成为一个元组。
    猜你喜欢
    • 2014-02-18
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    • 2016-07-31
    • 2017-03-26
    • 2021-08-05
    • 1970-01-01
    相关资源
    最近更新 更多