原文:http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/images.html
5.9. Images图像
有3种方法可以在你所编写的Tkinter程序中显示图像。
显示.xbm格式的位图(黑白两色)图像,参考章节 5.9.1, “The
BitmapImage
class”.显示.gif、.pgm或.ppm格式的全彩图像,请看章节 5.9.2, “The
PhotoImage
class”.Python图像库(PIL)支持图像格式更丰富。库里
设计
的ImageTk类是专门用来支持
Tkinter应用程序。请参照PIL的参考文档。
使用以下构造器可以显示.xbm格式的黑白图像。
tk.BitmapImage(file=f
[, background=b
][, foreground=c
])
是指 f
.xbm
图像的文件名。.
一般来说,图像表层比特(1)将会显示为黑色的像素点,底层比特(0)为透明。若要改变这种方式,使用可选参数background=
,设置底层的颜色b,可选参数b
foreground=
设置表层颜色c。颜色的指定,参考章节 5.3, “Colors”c
构造器将返回一个值,该值可用于任何Tkinter 需要图像位置。比如,要在标签(label)中显示图片,使用Label组件(参考章节
12, “The
Label
widget”)在BitmapImage对象的
image参数中输入上述图像值。
logo = tk.BitmapImage('logo.xbm', foreground='red') Label(image=logo).grid()