Python - 返回List的类型

时间:2012-06-05 02:09:23

标签: python list types undo

我正在尝试为我的图像编辑程序实现撤消功能。以下是我的代码的一部分:

def displayim(root, panel, img, editmenu):
    global image, L
    L.append(img)
    print(len(L))
    if (len(L) > 1):
        editmenu.entryconfig(0, state=NORMAL)
    else:
        editmenu.entryconfig(0, state=DISABLED)    
    image1 = ImageTk.PhotoImage(img)
    root.geometry("%dx%d+%d+%d" % (img.size[0], img.size[1], 200, 200))
    panel.configure(image = image1)
    panel.pack(side='top', fill='both', expand='yes')
    panel.image = image1
    image = img

def undo(root, panel, editmenu):
    global L
    i = len(L)
    del L[i-1]
    last = L.pop
    displayim(root, panel, last, editmenu)

我的想法是,当调用打开图像或添加图像效果的任何功能时,它将通过调用displayim来显示结果。参数editmenu确保如果没有要撤消的内容,将禁用undo命令。变量L是用于在调用每个函数后存储图像状态的列表。调用undo函数时,它将删除列表中的最后一项以及最后一项(现在成为最后一项)之前的项,并将此新的最后一项传递给displayim,以便程序可以显示图像的先前状态并再次将其添加到列表中。

但是,当我尝试使用undo函数时,我收到错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "D:\Python32\lib\tkinter\__init__.py", line 1399, in __call__
    return self.func(*args)
  File "D:\Users\ichigo\workspace\SS2\test\main.py", line 26, in <lambda>
    editmenu.add_command(label="Undo", command=lambda:file.undo(root, panel, editmenu), state=DISABLED)
  File "D:\Users\ichigo\workspace\SS2\test\file.py", line 51, in undo
    displayim(root, panel, last, editmenu)
  File "D:\Users\ichigo\workspace\SS2\test\file.py", line 39, in displayim
    image1 = ImageTk.PhotoImage(img)
  File "D:\Python32\lib\site-packages\PIL\ImageTk.py", line 110, in __init__
    mode = Image.getmodebase(mode)
  File "D:\Python32\lib\site-packages\PIL\Image.py", line 225, in getmodebase
    return ImageMode.getmode(mode).basemode
  File "D:\Python32\lib\site-packages\PIL\ImageMode.py", line 50, in getmode
    return _modes[mode]
TypeError: unhashable type: 'list'
Exception AttributeError: "'PhotoImage' object has no attribute '_PhotoImage__photo'" in <bound method PhotoImage.__del__ of <PIL.ImageTk.PhotoImage object at 0x01B1AA50>> ignored 

我猜错误意味着我从last传递给displayim的变量undo不是PIL图像对象,因此无法添加到PhotoImage。现在有什么解决方案吗?如果您有任何建议,请告诉我。

1 个答案:

答案 0 :(得分:4)

您应该将last = L.pop更改为last = L.pop()

L.pop返回<build-in method pop of list object>但不是PIL image object