Tkinter图像空白(无PIL)

时间:2019-10-25 09:30:52

标签: python image tkinter tkinter-canvas

我正在制作动画演示文稿,我想在其中添加图像,但是我不能使用PIL,因为它将在没有安装PIL的学校中演示,所以我使用了网站提供的方法http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm,但不起作用-我的幻灯片仍然空白。我该怎么办?

这是我的代码(几乎完全从网站复制了-我错过了吗?):

photo = PhotoImage('Alveoli.png')
label = Label(image=photo)
label.image = photo # keep a reference!
label.pack()

1 个答案:

答案 0 :(得分:3)

如果不使用外部图像库,则无法打开.png文件。由于PIL不可用,所以我认为其他类似的库也没有。

编辑:注意:PNG支持tkinter v8.6+文件。

想到的唯一可能性就是将图像转换为另一种兼容格式,例如GIFPGM

如果您使用这些格式之一的图像,则只需添加它们即可。这段代码对我来说效果很好:

from tkinter import *

root = Tk()

photo = PhotoImage(file="img.ppm")

img = Label(root, image=photo)
img.image = photo
img.place(x=0, y=0)

root.mainloop()