私は背景のためにいくつかのJPGファイルを利用するtkinterプログラムを書いています。しかし、私は、スクリプトが "pyinstaller"を使って.exeファイルに変換されると、tkinterウィンドウに使われる画像は.exeファイルにコンパイル/追加されないことを発見しました。Tkinterラベルでbase64でエンコードされたイメージ文字列を使用するにはどうすればよいですか?
したがって、私は外部依存関係がないようにPythonスクリプトでイメージをハードコードすることに決めました。
import base64
base64_encodedString= ''' b'hAnNH65gHSJ ......(continues...) '''
datas= base64.b64decode(base64_encodedString)
上記のコードはベース64符号化された画像データを復号化するために使用されます。この目的のために、私は次のことを行っています。 このデコードされた画像データを画像として使用し、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
あなたはpython2またはpython3を使用していますか?この[質問](http://stackoverflow.com/questions/27175029/tkinter-will-not-recognize-image-data-in-base64-encoded-string)によると、python3で可能であるようです。 –
@ j_4321私はPython 3を使用しています。私はその質問をチェックしましたが、私の問題を解決することはできません。 –
[質問](http://stackoverflow.com/questions/27175029/tkinter-will-not-recognize-image-data-in-base64-encoded-string)に記載されているコードを試しましたか? –