在Python 3.x中等价于PIL ImageTk
问题描述:
我正在开发一个Tkinter应用程序,它使用图标的png
图像文件数据库。为了在应用程序中使用这些图像,我使用PIL的Image.open
打开它们,通过ImageTk.PhotoImage
函数运行它,然后将它传递给窗口构件。在Python 3.x中等价于PIL ImageTk
问题是,我试图将我的整个项目移植到Python 3.x,并且由于PIL缺少对Python 3的支持,我不知道如何将图标加载到应用程序中。
如果有人知道一个解决方案,可以让我使用这些图标而无需将它们全部转换为.gif
位图,我将非常感激!
答
PNG文件,甚至与透明度,正确地在Linux上显示的Tkinter和TTK在Python 3.4.1,即使只有GIF和PPM/PGM支持documented。
以上PNG图像包含透明度。
from tkinter import *
root = Tk()
photo = PhotoImage(file="example.png")
photo_label = Label(image=photo)
photo_label.grid()
photo_label.image = photo
text = Label(text="Text") # included to show background color
text.grid()
root.mainloop()
上面的代码与透明度准确呈现图像所见如下:
请注意,画面是在设定中没有窗户的装饰和深色GUI颜色方案。