python - "_tkinter.TclError: image "pyimage4 "doesn' t 存在”

标签 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

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

是什么导致了这些错误?

最佳答案

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

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)

You must keep a reference to the image object in your Python program, either by storing it in a global variable, or by attaching it to another object.

Note: When a PhotoImage object is garbage-collected by Python (e.g. when you return from a function which stored an image in a local variable), the image is cleared even if it’s being displayed by a Tkinter widget. To avoid this, the program must keep an extra reference to the image object. A simple way to do this is to assign the image to a widget attribute, like this:

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

关于python - "_tkinter.TclError: image "pyimage4 "doesn' t 存在”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51220232/

相关文章:

python - HDF5格式的时间序列存储

c++ - Qt 5.2 - 如何将文本链接添加到 UI 中

java - 高级应用程序中的 JButton 如何工作?

python - pip线程安全吗?

python - 如何使用 Python 参数运行 PowerShell 脚本

python - 在 python 中渲染格式化文本(当前使用 pyglet)

python - 为什么 Python 允许在关键字参数后使用 *args?

Python在换行符处将随机整数写入文件

css - 水平 ul li 导航 Bootstrap

python - Numpy:从点列表中获取最大值的正确方法