Tkinter.PhotoImage 不支持 png 图像 [英] Tkinter.PhotoImage doesn't not support png image

查看:32
本文介绍了Tkinter.PhotoImage 不支持 png 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Tkinter 编写 GUI 并希望在 Tkiner.Label 中显示一个 png 文件.所以我有一些这样的代码:

I am using Tkinter to write a GUI and want to display a png file in a Tkiner.Label. So I have some code like this:

self.vcode.img = PhotoImage(data=open('test.png').read(), format='png')
self.vcode.config(image=self.vcode.img)

此代码在我的 Linux 机器上正确运行.但是当我在我的 Windows 机器上运行它时,它失败了.我也在其他几台机器上测试过(包括windows和linux),一直失败.

This code runs correctly on my Linux machine. But when I run it on my windows machine, it fails. I also tested on several other machines (include windows and linux), it failed all the time.

回溯是:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:Python27liblib-tkTkinter.py", line 1486, in __call__
    return self.func(*args)
  File "C:Documents and SettingsStclientGUI.py", line 150, in showrbox
    SignupBox(self, self.server)
  File "C:Documents and SettingsStclientGUI.py", line 197, in __init__
    self.refresh_vcode()
  File "C:Documents and SettingsStclientGUI.py", line 203, in refresh_vcode
    self.vcode.img = PhotoImage(data=open('test.png').read(), format='png')
  File "C:Python27liblib-tkTkinter.py", line 3323, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:Python27liblib-tkTkinter.py", line 3279, in __init__
   self.tk.call(('image', 'create', imgtype, name,) + options)
TclError: image format "png" is not supported

如果我删除源代码中的format='png',回溯将变成:

If I delete format='png' in the source code, the traceback will become:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:Python27liblib-tkTkinter.py", line 1486, in __call__
    return self.func(*args)
  File "C:Documents and SettingsStclientGUI.py", line 150, in showrbox
    SignupBox(self, self.server)
  File "C:Documents and SettingsStclientGUI.py", line 197, in __init__
    self.refresh_vcode()
  File "C:Documents and SettingsStclientGUI.py", line 203, in refresh_vcode
    self.vcode.img = PhotoImage(data=open('test.png').read())
  File "C:Python27liblib-tkTkinter.py", line 3323, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:Python27liblib-tkTkinter.py", line 3279, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
TclError: couldn't recognize image data

那么,我应该怎么做才能让它支持 png 文件?

So, what should I do to make it support png files?

推荐答案

tkinter 仅支持 GIF、PGM 和 PPM 3 种文件格式.您要么需要将文件转换为 .GIF 然后加载它们(要容易得多,但正如 jonrsharpe 所说,如果不先转换文件,什么都不会工作),或者您可以将您的程序移植到 Python 2.7 并使用 Python Imaging Library (PIL)及其使用 PNG 图像的 tkinter 扩展.

tkinter only supports 3 file formats off the bat which are GIF, PGM, and PPM. You will either need to convert the files to .GIF then load them (Far easier, but as jonrsharpe said, nothing will work without converting the file first) or you can port your program to Python 2.7 and use the Python Imaging Library (PIL) and its tkinter extensions to use a PNG image.

您可能会觉得有用的链接:http://effbot.org/tkinterbook/photoimage.htm

A link that you might find useful: http://effbot.org/tkinterbook/photoimage.htm

这篇关于Tkinter.PhotoImage 不支持 png 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆