博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python报错: _tkinter.TclError: couldn't recognize data in image file
阅读量:4677 次
发布时间:2019-06-09

本文共 1418 字,大约阅读时间需要 4 分钟。

在用Python创建画布canvas,并在画布上导入图像时报错:“_tkinter.TclError: couldn't recognize data in image file "F:\Python\test\a.gif"”

用tkinter只能装入GIF图片,也就是扩展名是.gif的图片文件,想要显示其他类型的图片,如png或jpg,需要用到其它模块 

def canvas_test():    import tkinter        window = tkinter.Tk()    window.geometry('600x400')    window.title('This is Canvas')        #创建550 * 300的画布    canvas = tkinter.Canvas(window, bg='green', width=550, height=300)        #在画布上创建图像,放置导入图片    image_file = tkinter.PhotoImage(file="F:\\Python\\test\\a.gif")    image = canvas.create_image(300, 10, anchor='n', image=image_file)    canvas.pack()        window.mainloop()

 在网上寻找解决办法,了解到更改图片后缀并不能修改图片格式。(网上参考:https://stackoverflow.com/questions/28740462/tkinter-couldnt-recognize-data-in-image-file)

所以,重新百度搜索一张GIF图片,下载后命名为c.gif(或者d.jpg),只要保存图片格式为GIF Image,再运行以下代码:

def canvas_test():    import tkinter        window = tkinter.Tk()    window.geometry('600x400')    window.title('This is Canvas')        #创建550 * 300的画布    canvas = tkinter.Canvas(window, bg='green', width=550, height=300)        #在画布上创建图像,放置导入图片    #image_file = tkinter.PhotoImage(file="F:\\gao\\Python\\test\\c.gif")    image_file = tkinter.PhotoImage(file="F:\\gao\\Python\\test\\d.jpg")    image = canvas.create_image(300, 10, anchor='n', image=image_file)        canvas.pack()        window.mainloop()

代码运行正常,图片显示正常,只是显示静态图片。

PhotoImage的图片检查只看图片本身的类型,与图片名称后缀无关。

转载于:https://www.cnblogs.com/Grace-gao/p/11340494.html

你可能感兴趣的文章
Exception occurred during processing request: id to load is required for loading
查看>>
go语言,chanel and goroutine(golang)(三)
查看>>
正则匹配、替换
查看>>
太阳能路灯软件设计
查看>>
二 面向对象
查看>>
Swift,下标简化方法的调用
查看>>
pal2nal
查看>>
HihoCoder - 1236 Scores (五维偏序,分块+bitset)
查看>>
Jquery 事件 DOM操作
查看>>
运算符
查看>>
FIR滤波器的verilog实现方法
查看>>
display的值和对应的意义
查看>>
HashSet、LinkHashSet、TreeSet总结
查看>>
手机号码输入格式化,数字三三四的输入;手机正则校验输入是否合理及提示;...
查看>>
抽象类
查看>>
CSS3 背景
查看>>
WPF DataGrid 之数据绑定
查看>>
c语言之gdb调试。
查看>>
位反转的最佳算法
查看>>
常用面试问题
查看>>