2016-06-28 35 views
1

這是我寫的代碼,我在其他用戶的幫助下修復了以前的錯誤。但是,這一個殘缺我。這是我的代碼如何在python3.4中修復ImageTk和Tkinter的AttributeError?

我在我認爲是問題的地區周圍放置了評論標記。爲了解決這個問題,我已經通讀了一些資料,但沒有發現任何結果。

import tkinter as tk 
import sys 
from timeit import default_timer as timer 
import time 
from PIL import ImageTk, Image 



tot_time=0 
del_time=0 
imgpath=r'logo.jpg' 

def NewTab(): 
    start=timer() 
    time.sleep(del_time) #user defined delay time 
    window.withdraw() 
    win2.deiconify() 
    end=timer() 
    tot_time=end-start 

    labW2.configure(text=('That took %s miliseconds or %s seconds'%(str(round(tot_time*1000,5)),str(round(tot_time,5))))) 

def close(): 
    sys.exit() 

def NTButt(): 
    win2.withdraw() 
    window.deiconify() 


win2=tk.Tk() 
win2.geometry('1100x900') 
win2.title('Button Click') 
labW2=tk.Label(win2, text='ERROR: Time could not be calculated') 
butW2=tk.Button(win2, text='Go back', bg='White', command=NTButt) 
btn2W2=tk.Button(win2, text='Leave', bg='Red', command=close) 
labW2.pack() 
butW2.pack() 
btn2W2.pack() 
win2.withdraw() 



window=tk.Tk() 
window.geometry('1100x900') 
window.title('Hello World') 
lab1= tk.Label(window, text='Input the desired delay time') 
ent=tk.Entry(window) 
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)) 
photo = ImageTk.PhotoImage(imgset) 
labimg = tk.Label(window, image=photo) 
labimg.image = photo 
labimg.pack() 
############################################### 
lab1.pack() 
ent.pack() 
btn.pack() 
btn2.pack() 

window.mainloop() 

虐待包括我收到

Traceback (most recent call last): 
    File "C:\PythonScripts\trunk\Personal\PythonWindow_ForTiming.py", line 53, in <module> 
    photo = ImageTk.PhotoImage(imgset) 
    File "C:\Python34\lib\site-packages\PIL\ImageTk.py", line 106, in __init__ 
    mode = Image.getmodebase(mode) 
    File "C:\Python34\lib\site-packages\PIL\Image.py", line 290, in getmodebase 
    return ImageMode.getmode(mode).basemode 
    File "C:\Python34\lib\site-packages\PIL\ImageMode.py", line 50, in getmode 
    return _modes[mode] 
KeyError: <PIL.ImageTk.PhotoImage object at 0x0000000002A06208> 
Exception ignored in: <bound method PhotoImage.__del__ of <PIL.ImageTk.PhotoImage object at 0x0000000002DC45C0>> 
Traceback (most recent call last): 
    File "C:\Python34\lib\site-packages\PIL\ImageTk.py", line 116, in __del__ 
    name = self.__photo.name 
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo' 

錯誤消息我是新來的Tkinter,這可能只是我的一個錯誤,請評論解決方案,幫助我理解。在此先感謝

回答

1

我不是TKinter專家,但它似乎是提出例外的行: ​​是不需要;您已經在前一行加載了圖像。

因爲你傳遞的ImageTk.PhotoImage()一個實例ImageTk.PhotoImage()

所以異常升高,有問題的代碼位變爲:

############################################### 
photo = ImageTk.PhotoImage(Image.open(imgpath)) 
labimg = tk.Label(window, image=photo) 
labimg.image = photo 
labimg.pack() 
############################################### 
+0

所以我這樣做,並得到了新的錯誤作爲結果,_tkinter.TclError:image「pyimage1」不存在...你也知道這個嗎?再次感謝 – Snowman

+2

的幫助,打開一個新的控制檯並重新運行它將工作的代碼。 – shivsn

+0

工作感謝你! – Snowman

相關問題