标签上未显示图像

我正在制作包含gif图像的标签。图像未显示,程序给出以下错误:

选择 | 换行 | 行号
  1. Exception in Tkinter callback
  2. Traceback (most recent call last):
  3.   File "C:\Python26\lib\lib-tk\Tkinter.py", line 1410, in __call__
  4.     return self.func(*args)
  5.   File "D:\7th sem\abiabi\pythoproj2\toplevelchanges.py", line 114, in about
  6.     label = Label(imageff,image=photo)
  7.   File "C:\Python26\lib\lib-tk\Tkinter.py", line 2464, in __init__
  8.     Widget.__init__(self, master, 'label', cnf, kw)
  9.   File "C:\Python26\lib\lib-tk\Tkinter.py", line 1930, in __init__
  10.     (widgetName, self._w) + extra + self._options(cnf))
  11. TclError: image "pyimage3" doesn't exist
  12.  

我的图像标签代码是:

选择 | 换行 | 行号
  1. imageff=Frame(root1)
  2. photo = PhotoImage(file="toolbar-search.gif")
  3. label = Label(imageff,image=photo)
  4. imageff["pady"]=10
  5. label.pack()
  6. imageff.pack()
  7. ......#other code of program
  8. root1.pack()#at the end
  9.  
# 回答1

选择 | 换行 | 行号
  1. photo = PhotoImage(file="/path/to/file/toolbar-search.gif")

若要开始调试,请尝试使用完整文件路径并回发任何其他错误。

# 回答2


我已经尝试了完整路径,但它给出了相同的错误。
# 回答3


Tkinter垃圾收集在函数返回后存储在局部变量中的图像对象,即使它正在显示。要避免这种情况,请通过创建属性来保留额外的引用。示例:

选择 | 换行 | 行号
  1. import Tkinter
  2. class Application(Tkinter.Frame):
  3.     def __init__(self, master=None):
  4.         Tkinter.Frame.__init__(self, master)
  5.         self.master = master
  6.         self.grid()
  7.         self.image = self.createWidgets()
  8.  
  9.     def createWidgets(self):
  10.         self.quitButton = Tkinter.Button (self, text='Quit',
  11.                                           command=self.quit)
  12.         self.quitButton.grid()
  13.  
  14.         img = Tkinter.PhotoImage(file="image.gif")
  15.         w = Tkinter.Label(self,image=img)
  16.         w.grid()
  17.         return img
  18.  
  19. root = Tkinter.Tk()
  20. app = Application(root)
  21. app.master.title("Sample application")
  22. root.mainloop()
# 回答4


这是正确的,但它只会创建一个空/空白标签。它不会给出"图像不存在错误",这只能表示一件事,即程序找不到图像。因此,这里有些东西没有意义;要么是发布的错误消息与实际消息不同,要么是用于查找图像的路径不正确。
# 回答5


原来的代码确实在我的电脑上显示了一个图像,所以它几乎必须是一个路径问题。

选择 | 换行 | 行号
  1. try:
  2.     import Tkinter as tk     ## Python 2.x
  3. except ImportError:
  4.     import tkinter as tk     ## Python 3.x
  5.  
  6. root1 = tk.Tk()
  7. imageff=tk.Frame(root1)
  8. fname='/home/david/python/Tkinter/Grayson/Examples/Chapter04/img52.gif'
  9. photo = tk.PhotoImage(file=fname)
  10. label = tk.Label(imageff,image=photo)
  11. label.photo=photo     ## keep a copy
  12. imageff["pady"]=10
  13. label.pack()
  14. imageff.pack()
  15. root1.mainloop() 
# 回答6


这不是路径问题,因为图像在那里,它是其他东西,并且"pyImag3"不存在是路径错误。我通过调用一个函数来显示图像标签,如果我删除图像,则显示其他所有内容,但使用标签则不显示。
# 回答7


纳粹泽拉,
你的问题解决了吗?
# 回答8


不,这个问题还没有解决。
# 回答9


Nazish Zehra--你会把你的代码和你收到的错误信息一起发布吗(你的代码足够多,我们可以测试它)?
# 回答10


下面是我要调用的函数

选择 | 换行 | 行号
  1. def about():
  2.         root1 = Tk()
  3.         root1.title("ABOUT")
  4.         root1["padx"] = 25
  5.         root1["pady"] = 25
  6.  
  7.  
  8.         TopLabel = Label(root1)
  9.  
  10.         TopLabel["text"] ="         STRING SEARCHER            "
  11.         TopLabel["font"]="Helvetica",12
  12.         TopLabel["relief"]= RIDGE
  13.         TopLabel.pack()
  14.  
  15.  
  16.         image_name = "toolbar-search.gif"
  17.         imageff =PhotoImage(file=image_name)
  18.         imageff=Frame(root1)
  19.         label = Label(imageff,image=photo)
  20.         label.image = photo 
  21.         imageff["pady"]=10
  22.         label.pack()
  23.         imageff.pack()
  24.         root1.mainloop()
  25.  

接下来,我将从相同的程序中调用它。通过制作菜单栏来调用该函数。代码为:

选择 | 换行 | 行号
  1. root = Tk()
  2. menubar = Menu(root)
  3. helpmenu = Menu(menubar, tearoff=0)
  4. helpmenu.add_command(label="About...", command=about)
  5. root.config(menu=menubar)
  6.  
  7. root.mainloop()
  8.  

并且错误是相同的,即"pyImage3"不存在

# 回答11

选择 | 换行 | 行号
  1.         label = Label(imageff,image=photo)

程序中尚未声明变量"PHOTO"。此外,Imagef还被使用了两次

选择 | 换行 | 行号
  1.         imageff =PhotoImage(file=image_name)
  2.         imageff=Frame(root1) 

再看一看上面发布的工作代码。

标签: python

添加新评论