python _tkinter.TclError: couldn‘t recognize data in image file
问题描述——tkinter.TclError: couldn‘t recognize data in image file
问题原因
出现该问题的原因是tkinter.PhotoImage()仅支持GIF and PGM/PPM 文件格式等几种不常用的图片格式问题
如下:
bm=PhotoImage(file=r'D:\\a\\aa.jpg')
如果想用 ".jpg"文件格式,直接用上面的代码,会报“couldn’t recognize data in image file "bm=PhotoImage(file=r’D:\a\aa.jpg’)"错误。
而且直接修改图片后缀为.gif格式也会出现这样的问题,修改方法只能从根本上修改如下(用ImageTK)
gif文件以及png文件可以借助PhotoImage()方法。这是Tkinter方法, 这意味着你无需导入任何其他模块即可使用。
photo = PhotoImage(file