【发布时间】:2017-03-16 12:53:00
【问题描述】:
我在我的 GUI 中使用了一个图像,当我编译 .exe 或我的 .py 时,我不希望它作为外部资源。 我想我应该将它编码为一个字符串,复制代码中的字符串并对其进行解码并将其提供给 Tkinter。试了很多办法还是不行,代码如下:
import tkinter as tk
from tkinter import filedialog
from tkinter import *
import PIL
from PIL import Image, ImageTk
import base64
stringimagine="something the print gave me"
imagine=open('logo.jpg','rb')
encoded=base64.b64encode(imagine.read())
print(encoded)
imagine2=base64.b64decode(stringimagine)
fereastra_principala = tk.Tk()
poza=Label(fereastra_principala,image=imagine2)
poza.pack(fill='both',expand='yes')
fereastra_principala.mainloop()
对于此代码,我收到此错误:
File "C:\Python34\lib\tkinter\__init__.py", line 2609, in __init__ Widget.__init__(self, master, 'label', cnf, kw)
File "C:\Python34\lib\tkinter\__init__.py", line 2127, in __init__ (widgetName, self._w) + extra + self._options(cnf))_tkinter.TclError
这就是我现在用来获取照片的方式,作为外部资源:
img=Image.open('logo.jpg')
image=ImageTk.PhotoImage(img)
poza=Label(fereastra_principala,image=image)
poza.pack()
【问题讨论】:
标签: python string tkinter base64 encode