2016-06-27 20 views
1

イメージがあるウィンドウを生成できるtkinterコードを作成しようとしています。これは私にエラーを与え続けエリアです:jpegをtkinter(python3.4)に配置する方法は?

のImagePathは、マイピクチャフォルダ

に画像へのパスであり、これは私が

Traceback (most recent call last): 
    File "C:\PythonScripts\trunk\Personal\PythonWindow_ForTiming.py", line 49, in <module> 
    img = tk.Label(window, image=imgset) 
    File "C:\Python34\lib\tkinter\__init__.py", line 2604, in __init__ 
    Widget.__init__(self, master, 'label', cnf, kw) 
    File "C:\Python34\lib\tkinter\__init__.py", line 2122, in __init__ 
    (widgetName, self._w) + extra + self._options(cnf)) 
_tkinter.TclError: image "pyimage1" doesn't exist 

を得続ける誤りである

window=tk.Tk() 
window.geometry('1100x900') 
window.title('Hello World') 
lab1= tk.Label(window, text='Input the desired delay time') 
btn=tk.Button(window, text='Go to new window', bg='Blue', command=NewTab) 
btn2=tk.Button(window, text='Leave', bg='Red', command=close) 
imgset=ImageTk.PhotoImage(Image.open(imgpath)) 
img = tk.Label(window, image=imgset) 
img.pack() 

lab1.pack() 
btn.pack() 
btn2.pack() 

window.mainloop() 

私は間違って何をしていますか?あなたは私はちょうどあなたが以下のように2行を追加する必要があり

答えて

0

事前にTkinterの約

感謝を学んでいます、私は理解を助けるために、コメントを含めてもらえ

imgset=Image.open(imgpath) 
# Convert imgset to a Tkinter-compatible image object 
photo = ImageTk.PhotoImage(imgset) 
img = tk.Label(window, image=photo) 
# Keep a reference to the image 
img.image = photo 
img.pack() 

(Iは、問題の行をコメント)小学生のノート:imgの名前をLabel()インスタンスをよりよく反映する名前(最終的な混乱を避けるために)をlabel

などの名前に変更することができます。The Tkinter PhotoImage Class

+1

ありがとうございました。非常に有益だった – Snowman

関連する問題