我正在尝试使用 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
没有它的 parentroot
时,不会发生编译错误,但 GUI 不显示任何图像!
谁能确定可能是什么问题?