我从Python 3.4更改为Python 2.7,在调试我的脚本时,我发现解释器无法读取字符串中编码的base64图形。我想使用编码图像作为tkinter画布的背景,但我现在无法像在Python 3.4中那样这样做:
background="""
iVBORw0KGgoAAA #....continues
"""
photo = tk.PhotoImage(data=background)
width, height = photo.width(), photo.height()
canvas = tk.Canvas(root, width=width, height=height, bd=-2)
canvas.pack()
canvas.create_image(0, 0, image=photo, anchor="nw")
当我运行脚本时,我收到此错误:
>Traceback (most recent call last):
File "main.py", line 31, in <module>
photo = tk.PhotoImage(data=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 image data