无法在 Tkinter 中显示图像 [英] Cannot Display an Image in Tkinter
本文介绍了无法在 Tkinter 中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我运行程序时,画布会出现,但图像不会.
When I run the program the canvas shows up but the Image does not.
canvas = Canvas(frame, width = 128, height = 128, bg= 'white')
image_data = Image.open('NoArt.gif')
ppm_f = ImageTk.PhotoImage(image_data)
canvas.create_image(0, 0, image = ppm_f, anchor = NW)
canvas.pack(side=BOTTOM)
有什么想法吗??
附注.
我有PIL 1.6 版,python 2.6,和python 2.6自带的Tkinter版本
I have PIL ver 1.6, python 2.6, and The Version of Tkinter that comes with python 2.6
推荐答案
好的,我想通了.显然由于python处理垃圾处理的方式,图片只是被删除了.需要在全局范围内引用图像.这是我最终使用的工作代码:
Ok, I figured It out. Apparently due to the way that python deals with garbage disposal the pictures just get erased. A reference to the image in the global scope is required. This is the working code I eventually ended up using:
self.photo = PhotoImage(file="noart.ppm")
self.Artwork = Label(self.frame, image=self.photo)
self.Artwork.photo = self.photo
self.Artwork.pack()
self.Artwork.photo = self.photo
是重要的部分.它确保将显示图像.
that self.Artwork.photo = self.photo
is the important part. It ensures that the Image will be shown.
这篇关于无法在 Tkinter 中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文