【问题标题】:"_tkinter.TclError: image "pyimage4" doesn't exist" [duplicate]“_tkinter.TclError:图像“pyimage4”不存在”[重复]
【发布时间】:2018-07-07 05:35:32
【问题描述】:

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

#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

如果我将代码 sn-p 复制到一个新文件中,则没有问题。

是什么导致了这些错误?

【问题讨论】:

  • 请创建一个minimal reproducible example,现在,您的代码是部分的。我们需要能够重现您的问题,因此您需要包含所有导入并创建一个仍然会引发该异常的尽可能小的示例。
  • 另外,你为什么不直接导入py文件呢?为什么exec? (至少你可以改用execfile()
  • @abccd 我已经删除了高管,仍然遇到同样的问题

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


【解决方案1】:

在您最近的编辑中,您提到代码在一个函数中,这使一切变得不同。

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()

【讨论】:

  • 保存对图像对象的额外引用的最佳位置在哪里?当它在函数内时,它会不断收集垃圾。
  • 答案是:lbl.image = photo
猜你喜欢
  • 1970-01-01
  • 2019-06-12
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 2022-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多