【问题标题】:cannot associate image to tkinter label无法将图像关联到 tkinter 标签
【发布时间】:2014-08-23 09:20:55
【问题描述】:

我正在尝试使用 tkinter.Label() 小部件向 tkinter GUI 显示图像。过程看似简单明了,但这段代码行不通!

代码:

import Tkinter as tk
import Image, ImageTk, sys

filename = 'AP_icon.gif'
im = Image.open(filename) # Image is loaded, because the im.show() works

tkim = ImageTk.PhotoImage(im)

root = tk.Tk()

label = tk.Label(root, image = tkim) # Here is the core problem (see text for explanation)
label.image = tkim # This is where we should keep the reference, right?
label.grid (row = 0, column = 0)

tk.Button(root, text = 'quit', command = lambda: sys.exit()).grid(row = 1, column = 1)
root.mainloop()

当我们执行这段代码时,它没有编译,报错:

TclError: image "pyimage9" doesn't exist

当我定义 label 没有其父 root 时,不会发生编译错误,但 GUI 不显示任何图像!

谁能确定可能是什么问题?

【问题讨论】:

  • 如果我交换 root = tk.Tk()tkim = ImageTk... 语句的顺序,您的代码对我有效。
  • 谢谢。但我尝试交换语句,但同样的错误再次重复。还有其他可能的原因吗?
  • 你确定上面的代码给出了你所说的错误吗?通常内部图像从零开始编号,但您声称错误提及“pyimage9”,即使您的代码尝试创建单个图像。
  • 很高兴你问。事实上,编号对我来说是从 1 开始的。但是当我再次运行时(我在 ubuntu 12.04 中使用 ipython),数字会更新为 2,3 等。我复制的错误是我第 9 次尝试运行类似的代码。

标签: python user-interface tkinter photoimage


【解决方案1】:

当我们尝试在 Ipython 中运行上述代码时会发生此问题。并且可以通过换行来解决

root = tk.Tk() to

root = tk.Toplevel()

【讨论】:

  • 这并不是一个真正正确的修复方法,因为您依赖于在创建第一个小部件时创建的隐式根窗口。你最终会得到一个变量root,它实际上并不是真正的根窗口。
  • @BryanOakley,感谢您的反馈。截至目前,我正在以这种方式管理这个问题。有没有更好的解决方案或适当的解决方案来解决这个问题?
  • 我已经给出了一个简短的答案:您需要在调用任何其他 tkinter 命令之前显式创建根窗口。
【解决方案2】:

在调用任何其他 tkinter 函数之前,您需要创建根小部件。将root的创建移到图片创建之前。

【讨论】:

  • 谢谢。但是我已经尝试在开始时创建根目录,但同样的错误再次重复。会不会有其他可能的原因?似乎上面的@unutbu 让代码与开关一起工作。
【解决方案3】:

我在 tkinter 中显示图像的一般方式是:

import Tkinter as tk
root = tk.Tk()
image1 = tk.PhotoImage(file = 'name of image.gif')
# If image is stored in the same place as the python code file,
# otherwise you can have the directory of the image file.
label = tk.Label(image = image1)
label.image = image1 # yes can keep a reference - good!
label.pack()
root.mainloop()

在上述情况下它可以工作,但你有类似的东西:

import Tkinter as tk
image = tk.PhotoImage(file = 'DreamPizzas.gif') #here this is before root = tk.Tk()
root = tk.Tk()
# If image is stored in the same place as the python code file,
# otherwise you can have the directory of the image file.
label = tk.Label(image = image)
label.image = image
label.pack()
root.mainloop()

这给了我一个runtime error: too early to create image.

但是您说您的错误是 image pyimage9 不存在,这很奇怪,因为在顶部您已将 filename 设置为“AP_icon.gif”,因此您会认为您会收到不同的错误我不知道pyimage9 来自哪里。这让我觉得也许你在某处得到了不正确的文件名?您还需要将root = tk.Tk() 移到导入下的顶部。

【讨论】:

  • 你说的代码为你工作(上面的那个)给了我一个空白的 GUI,可能是因为 label 是在没有父 root 的情况下定义的。但是当我用父级root 定义它时,同样的错误TclError: image "pyimage1" doesn't exist 再次出现。即使我在定义root 之前定义了image1,我也会得到相同的错误,而不是您指定的runtime error。文件名是正确的,因为如代码中所述,im.show() 确实显示了图像。
  • 那很奇怪,定义没有父root的标签就可以了,除非有其他tkinter窗口同时运行,你是这样做的吗?也许要解决这个问题,您应该制作额外的窗口 Toplevel()...?
  • 是的,很奇怪,不是吗?我刚才试过Toplevel(root)。它没有帮助。同样的错误! :(
  • 不知道,你用的是什么IDE?
  • python 2.7.3,使用 IPython 0.12.1,操作系统 Ubuntu 12.04
【解决方案4】:

重新启动内核以消除错误“TclError:图像“pyimage9”不存在”

【讨论】:

    【解决方案5】:

    尝试以下代码,因为我能够纠正相同的错误:

    window=Tk()
    c=Canvas(window,height=2000,width=2000)
    p=PhotoImage(file='flower1.gif',master = c)
    c.create_image(500,500,image=p)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-25
      • 1970-01-01
      • 2022-01-18
      相关资源
      最近更新 更多