Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python &引用_tkinter.TclError:image";pyimage4“;不';“不存在”;_Python_Python 3.x_User Interface_Tkinter - Fatal编程技术网

Python &引用_tkinter.TclError:image";pyimage4“;不';“不存在”;

Python &引用_tkinter.TclError:image";pyimage4“;不';“不存在”;,python,python-3.x,user-interface,tkinter,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="

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

#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
如果我将代码段复制到一个新文件中,就不会有问题


是什么导致了这些错误

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

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

如建议的那样,您可以执行以下操作:

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

请创建一个,现在,您的代码是部分的。我们需要能够重现您的问题,因此您需要包括所有导入,并创建一个仍然引发该异常的最小可能示例。另外,为什么不导入py文件呢?为什么
exec
?(至少你可以使用
execfile()
来代替)@abccd我已经删除了execs,仍然会遇到同样的问题:在哪里保存对图像对象的额外引用是最好的?当它在函数中时,会不断地收集垃圾。答案如下:
lbl.image=photo