“ _tkinter.TclError:图像“ pyimage4”不存在”

时间:2018-07-07 05:35:32

标签: python python-3.x user-interface tkinter

这是我在函数中调用的代码的一部分:

#Labels and Window layout
lsfpy = Tk()
lsfpy.title("Helicopters Sydney")
lsfpy.resizable(False, False)
Label(lsfpy, text="Locations in Sydney").grid(row=0)
Label(lsfpy, text="To").grid(column = 1, row=1, sticky=N)
Label(lsfpy, text="From").grid(column = 1, row=2, sticky = W)
Label(lsfpy, text="").grid(column = 1, row=3)
Label(lsfpy, text="Date").grid(column = 1, row=4, sticky=SW)
Label(lsfpy, text="Time").grid(column = 1, row=5, sticky=SW)

#Map
photo = PhotoImage(file = 'GUI Files/Map/Sydmap.gif')
photo = photo.subsample(2)
lbl = Label(lsfpy,image = photo)
lbl.grid(column=0, row=3)

运行它时,出现此错误:

    Exception in Tkinter callback
    Traceback (most recent call last):
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 1702, in __call__
    return self.func(*args)
  File "/Users/62633/Documents/2018/SDD/Webdrone Sydney/Freight.py", line 22, in calculateandnext
    saveandgotomapf(tp,am1,am2,am3,am4,am5)
  File "/Users/62633/Documents/2018/SDD/Webdrone Sydney/Freight.py", line 55, in saveandgotomapf
    locationfreight(fdpy)
  File "/Users/62633/Documents/2018/SDD/Webdrone Sydney/Locationfreight.py", line 192, in locationfreight
    lbl = Label(lsfpy,image = photo)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 2763, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 2296, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage4" doesn't exist

当我注释掉

 photo = photo.subsample(2)

错误稍有更改为:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 1702, in __call__
    return self.func(*args)
  File "/Users/62633/Documents/2018/SDD/Webdrone Sydney/Freight.py", line 22, in calculateandnext
    saveandgotomapf(tp,am1,am2,am3,am4,am5)
  File "/Users/62633/Documents/2018/SDD/Webdrone Sydney/Freight.py", line 55, in saveandgotomapf
    locationfreight(fdpy)
  File "/Users/62633/Documents/2018/SDD/Webdrone Sydney/Locationfreight.py", line 192, in locationfreight
    lbl = Label(lsfpy,image = photo)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 2763, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 2296, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage3" doesn't exist

如果我将代码片段复制到新文件中,则没有问题。

是什么导致这些错误?

1 个答案:

答案 0 :(得分:0)

在您最近的编辑中,您提到代码在一个函数中,这与众不同。

PhotoImage不是由tkinter保留的,因此在函数返回后,Python的垃圾收集器吞噬图像之前,必须保留对其的引用。当它出现时,tkinter不再能够找到您的图像,因此您的错误提示该图像不存在。

如建议使用effbot,则可以执行以下操作:

photo = PhotoImage(file = 'GUI Files/Map/Sydmap.gif')
photo = photo.subsample(2)
lbl = Label(lsfpy,image = photo)
lbl.image = photo
lbl.grid(column=0, row=3)
  

您必须在Python程序中保留对图像对象的引用,   通过将其存储在全局变量中或将其附加到   另一个对象。

     

注意:当PhotoImage对象被垃圾回收时,   Python(例如,当您从将图片存储在其中的函数返回时,   一个局部变量),即使正在显示该图片,也会清除该图片   通过Tkinter小部件。为了避免这种情况,程序必须保留一个额外的   引用图像对象。一种简单的方法是分配   将该图像添加到小部件属性,如下所示:

label = Label(image=photo)
label.image = photo # keep a reference! label.pack()