【发布时间】:2017-06-29 17:20:42
【问题描述】:
我正在编写一个使用一些 JPG 文件作为其背景的 tkinter 程序。但是,我发现当使用“pyinstaller”将脚本转换为 .exe 文件时,用于 tkinter 窗口的图像不会被编译/添加到 .exe 文件中。
因此,我决定在 Python 脚本中对图像进行硬编码,这样就没有外部依赖。为此,我做了以下几件事:
import base64
base64_encodedString= ''' b'hAnNH65gHSJ ......(continues...) '''
datas= base64.b64decode(base64_encodedString)
以上代码用于解码base 64编码的Image数据。 我想将此解码后的图像数据用作图片并在 tkinter 中显示为标签/按钮。
例如:
from tkinter import *
root=Tk()
l=Label(root,image=image=PhotoImage(data=datas)).pack()
root.mainloop()
但是,tkinter 不接受将存储在data 中的值用作图像。
它显示以下错误 -
Traceback (most recent call last):
File "test.py", line 23, in <module>
l=Label(root,image=PhotoImage(data=datas))
File "C:\Users\Admin\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 3394, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 3350, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize image data
【问题讨论】:
标签: python image tkinter base64 pyinstaller