我最近切换到了Python 2.7(来自3.4),而且我在使用Tkinter Photoimage类读取.png文件时遇到了麻烦。我说:
background = "background.png"
photo = tk.PhotoImage(file=background)
但是我收到了这个错误:
Traceback (most recent call last):
File "main.py", line 36, in <module>
photo = tk.PhotoImage(file=background)
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)
_tkinter.TclError: couldn't recognize data in image file "background.png"
答案 0 :(得分:1)
img = ImageTk.PhotoImage(Image.open(background))