2016-03-10 130 views
1

我剛開始使用python tkinter,並且我有一個打開一個新窗口的按鈕。一個新窗口有一個圖像,但圖像不會顯示出來。請你幫我解決我的問題嗎?(Tkinter)圖片將無法顯示在新窗口中

from tkinter import * 

def nwindow(): 
    nwin = Toplevel() 
    nwin.title("New Window") 
    btn.config(state = 'disable') 

    photo2 = PhotoImage(file = 'funny.gif') 
    lbl2 = Label(nwin, image = photo2) 
    lbl2.pack() 

def quit(): 
    nwin.destroy() 
    btn.config(state = 'normal') 

qbtn = Button(nwin, text = 'Quit', command = quit) 
qbtn.pack() 

main = Tk() 
main.title("Main Window") 
main.geometry("750x750") 

photo = PhotoImage(file = 'funny.gif') 
lbl = Label(main, image = photo) 
lbl.pack() 

btn = Button(main, text = "New Winodw", command = nwindow) 
btn.pack() 

main.mainloop() 
+2

這可能是有用的:爲什麼我的Tkinter的圖像不會出現?](HTTP:// effbot .org/pyfaq/why-do-my-tkinter-images-not-appear.htm) – Kevin

回答

0

您的編碼不工作,但把.mainloop()應該解決您的問題

def nwindow(): 
    nwin = Toplevel() 
    nwin.title("New Window") 
    btn.config(state = 'disable') 

    photo2 = PhotoImage(file = 'funny.gif') 
    lbl2 = Label(nwin, image = photo2) 
    lbl2.pack() 
    nwin.mainloop() 
+0

Thankyou sooooo much !!!!!!!!! –