我正在制作包含gif图像的标签。图像未显示,程序给出以下错误:
- Exception in Tkinter callback
- Traceback (most recent call last):
- File "C:\Python26\lib\lib-tk\Tkinter.py", line 1410, in __call__
- return self.func(*args)
- File "D:\7th sem\abiabi\pythoproj2\toplevelchanges.py", line 114, in about
- label = Label(imageff,image=photo)
- File "C:\Python26\lib\lib-tk\Tkinter.py", line 2464, in __init__
- Widget.__init__(self, master, 'label', cnf, kw)
- File "C:\Python26\lib\lib-tk\Tkinter.py", line 1930, in __init__
- (widgetName, self._w) + extra + self._options(cnf))
- TclError: image "pyimage3" doesn't exist
-
我的图像标签代码是:
- imageff=Frame(root1)
- photo = PhotoImage(file="toolbar-search.gif")
- label = Label(imageff,image=photo)
- imageff["pady"]=10
- label.pack()
- imageff.pack()
- ......#other code of program
- root1.pack()#at the end
-
# 回答1 - photo = PhotoImage(file="/path/to/file/toolbar-search.gif")
若要开始调试,请尝试使用完整文件路径并回发任何其他错误。
# 回答2
我已经尝试了完整路径,但它给出了相同的错误。
# 回答3
Tkinter垃圾收集在函数返回后存储在局部变量中的图像对象,即使它正在显示。要避免这种情况,请通过创建属性来保留额外的引用。示例:
- import Tkinter
- class Application(Tkinter.Frame):
- def __init__(self, master=None):
- Tkinter.Frame.__init__(self, master)
- self.master = master
- self.grid()
- self.image = self.createWidgets()
-
- def createWidgets(self):
- self.quitButton = Tkinter.Button (self, text='Quit',
- command=self.quit)
- self.quitButton.grid()
-
- img = Tkinter.PhotoImage(file="image.gif")
- w = Tkinter.Label(self,image=img)
- w.grid()
- return img
-
- root = Tkinter.Tk()
- app = Application(root)
- app.master.title("Sample application")
- root.mainloop()
# 回答4
这是正确的,但它只会创建一个空/空白标签。它不会给出"图像不存在错误",这只能表示一件事,即程序找不到图像。因此,这里有些东西没有意义;要么是发布的错误消息与实际消息不同,要么是用于查找图像的路径不正确。
# 回答5
原来的代码确实在我的电脑上显示了一个图像,所以它几乎必须是一个路径问题。
- try:
- import Tkinter as tk ## Python 2.x
- except ImportError:
- import tkinter as tk ## Python 3.x
-
- root1 = tk.Tk()
- imageff=tk.Frame(root1)
- fname='/home/david/python/Tkinter/Grayson/Examples/Chapter04/img52.gif'
- photo = tk.PhotoImage(file=fname)
- label = tk.Label(imageff,image=photo)
- label.photo=photo ## keep a copy
- imageff["pady"]=10
- label.pack()
- imageff.pack()
- root1.mainloop()
# 回答6
这不是路径问题,因为图像在那里,它是其他东西,并且"pyImag3"不存在是路径错误。我通过调用一个函数来显示图像标签,如果我删除图像,则显示其他所有内容,但使用标签则不显示。
# 回答9
Nazish Zehra--你会把你的代码和你收到的错误信息一起发布吗(你的代码足够多,我们可以测试它)?
# 回答10
下面是我要调用的函数
- def about():
- root1 = Tk()
- root1.title("ABOUT")
- root1["padx"] = 25
- root1["pady"] = 25
-
-
- TopLabel = Label(root1)
-
- TopLabel["text"] =" STRING SEARCHER "
- TopLabel["font"]="Helvetica",12
- TopLabel["relief"]= RIDGE
- TopLabel.pack()
-
-
- image_name = "toolbar-search.gif"
- imageff =PhotoImage(file=image_name)
- imageff=Frame(root1)
- label = Label(imageff,image=photo)
- label.image = photo
- imageff["pady"]=10
- label.pack()
- imageff.pack()
- root1.mainloop()
-
接下来,我将从相同的程序中调用它。通过制作菜单栏来调用该函数。代码为:
- root = Tk()
- menubar = Menu(root)
- helpmenu = Menu(menubar, tearoff=0)
- helpmenu.add_command(label="About...", command=about)
- root.config(menu=menubar)
-
- root.mainloop()
-
并且错误是相同的,即"pyImage3"不存在
# 回答11 - label = Label(imageff,image=photo)
程序中尚未声明变量"PHOTO"。此外,Imagef还被使用了两次
- imageff =PhotoImage(file=image_name)
- imageff=Frame(root1)
再看一看上面发布的工作代码。