python – Tkinter.PhotoImage不支持png图像
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – Tkinter.PhotoImage不支持png图像,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2278字,纯文字阅读大概需要4分钟。
内容图文
![python – Tkinter.PhotoImage不支持png图像](/upload/InfoBanner/zyjiaocheng/713/494a5404ee5945f99dfcfb3d346ff88c.jpg)
我正在使用Tkinter编写GUI并希望在Tkiner.Label中显示png文件.
所以我有一些像这样的代码:
self.vcode.img = PhotoImage(data=open('test.png').read(), format='png')
self.vcode.config(image=self.vcode.img)
此代码在我的Linux机器上正确运行.但是当我在我的Windows机器上运行它时,它失败了.我还在其他几台机器上测试过(包括windows和linux),它一直都失败了.
Traceback是:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1486, in __call__
return self.func(*args)
File "C:\Documents and Settings\St\client\GUI.py", line 150, in showrbox
SignupBox(self, self.server)
File "C:\Documents and Settings\St\client\GUI.py", line 197, in __init__
self.refresh_vcode()
File "C:\Documents and Settings\St\client\GUI.py", line 203, in refresh_vcode
self.vcode.img = PhotoImage(data=open('test.png').read(), format='png')
File "C:\Python27\lib\lib-tk\Tkinter.py", line 3323, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 3279, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
TclError: image format "png" is not supported
如果我在源代码中删除format =’png’,则回溯将变为:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1486, in __call__
return self.func(*args)
File "C:\Documents and Settings\St\client\GUI.py", line 150, in showrbox
SignupBox(self, self.server)
File "C:\Documents and Settings\St\client\GUI.py", line 197, in __init__
self.refresh_vcode()
File "C:\Documents and Settings\St\client\GUI.py", line 203, in refresh_vcode
self.vcode.img = PhotoImage(data=open('test.png').read())
File "C:\Python27\lib\lib-tk\Tkinter.py", line 3323, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 3279, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
TclError: couldn't recognize image data
那么,我该怎么做才能使它支持png文件?
解决方法:
tkinter仅支持3种文件格式,即GIF,PGM和PPM.您将需要将文件转换为.GIF然后加载它们(更容易,但正如jonrsharpe所说,没有首先转换文件就没有任何工作)或者您可以将程序移植到Python 2.7并使用Python Imaging Library(PIL)和它的tkinter扩展使用PNG图像.
您可能会发现有用的链接:http://effbot.org/tkinterbook/photoimage.htm
内容总结
以上是互联网集市为您收集整理的python – Tkinter.PhotoImage不支持png图像全部内容,希望文章能够帮你解决python – Tkinter.PhotoImage不支持png图像所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。