Tkinter控件使用image的问题

写一个gui的小程序,想在Button控件里插入一张图片

photo=PhotoImage(file=self.path + '/1.gif')
wifi_button = Label(top_frame, text="WIFI",image=photo)

gif格式图片没有问题,但是png及jpg格式无法插入,报错:

_tkinter.TclError: couldn't recognize data in image file

谷歌一番后发现Tkinter只支持gif和bmp等少数几种格式,要插入其它格式的图片就需要使用PIL处理一下了。

from PIL import Image, ImageTk

wifi_img = Image.open(self.path + '/img/2.jpg')
photo=ImageTk.PhotoImage(wifi_img)

另外需要注意一下,pip安装PIL的命令是

pip install pillow #不是pip install PIL,反正我是没装上^_^

你可能感兴趣的:(python,tkinter)