2012-06-17 44 views
2

我正在开发一个Tkinter应用程序,它使用图标的png图像文件数据库。为了在应用程序中使用这些图像,我使用PIL的Image.open打开它们,通过ImageTk.PhotoImage函数运行它,然后将它传递给窗口构件。在Python 3.x中等价于PIL ImageTk

问题是,我试图将我的整个项目移植到Python 3.x,并且由于PIL缺少对Python 3的支持,我不知道如何将图标加载到应用程序中。

如果有人知道一个解决方案,可以让我使用这些图标而无需将它们全部转换为.gif位图,我将非常感激!

回答

0

您可以使用Pillow与Python 3.3或更早版本的png图像一起使用。

here摘自:

枕头> = 2.0.0支持Python版本:2.6,2.7,3.2,3.3。枕头< 2.0.0支持Python版本:2.4,2.5,2.6,2.7。

4

PNG文件,甚至与透明度,正确地在Linux上显示的Tkinter和TTK在Python 3.4.1,即使只有GIF和PPM/PGM支持documented

Example PNG File With Transparency And Alpha

以上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() 

上面的代码与透明度准确呈现图像所见如下:

PNG Test Image In Python 3.4.1 tkinter with text

请注意,画面是在设定中没有窗户的装饰和深色GUI颜色方案。