本文介绍了无法在Tkinter中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我运行程序时,画布会显示,但Image不显示。
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)
任何想法??
PS。
我有
PIL版本1.6,
python 2.6和
python 2.6附带的Tkinter版本
I havePIL ver 1.6,python 2.6, andThe 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中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!