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

时间:2017-09-03 18:24:56

标签: python

这是我的代码 -

import mouse
import pickle

#https://github.com/boppreh/keyboard/blob/master/keyboard/mouse.py


with open ('outfile', 'rb') as fp:
    itemlist = pickle.load(fp)


print (type (itemlist[0]))
print (itemlist[:5])

itemlist[0][1] = 5

这是输出:

<class 'mouse._mouse_event.MoveEvent'>
[MoveEvent(x=1166, y=56, time=1504454256.95986), MoveEvent(x=1161, y=60, time=1504454256.9678605), MoveEvent(x=1158, y=63, time=1504454256.975861), MoveEvent(x=1150, y=67, time=1504454256.9838612), MoveEvent(x=1146, y=70, time=1504454256.9918618)]
Traceback (most recent call last):
  File "C:\Python\Move Mouse\mouse mod\mouse_mod_open.py", line 14, in <module>
    itemlist[0][1] = 5
TypeError: 'MoveEvent' object does not support item assignment

我是python的新手,我想尝试在上面的列表中为x和y重新分配值。

1 个答案:

答案 0 :(得分:1)

Since MoveEvent is a namedtuple,您需要访问MoveEvent的属性,并制作一个新属性。

# Select the first item of itemlist
old_event = itemlist[0]
# Create the new event
new_event = MoveEvent(x=5, y=1500, time=old_event.time)

根据需要更改您的值。