[英]Python - Return type of List
我正在尝试为我的图像编辑程序实现撤消功能。 以下是我的代码的一部分:
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
从undo
传递给displayim
的变量不是PIL图像对象,因此无法将其添加到PhotoImage
。 现在有什么可用的解决方案吗? 如果您有任何建议,请告诉我。
您应该将last = L.pop
更改为last = L.pop()
L.pop
返回<build-in method pop of list object>
但不返回PIL image object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.