2017-06-05 66 views
0
import Tkinter as Tk 

root = Tk.Tk() 

def generate(): 
    root.quit() 

label1 = Tk.Label(root, text="Map Width:") 
label2 = Tk.Label(root, text="Map Height:") 
label3 = Tk.Label(root, text="Lake Size:") 
e1 = Tk.Entry(root) 
e2 = Tk.Entry(root) 
e3 = Tk.Entry(root) 
button1 = Tk.Button(root, text="Generate", command=generate) 
label1.grid(row=0, column=0) 
label2.grid(row=1, column=0) 
label3.grid(row=2, column=0) 
e1.grid(row=0, column=1) 
e2.grid(row=1, column=1) 
e3.grid(row=2, column=1) 
button1.grid(row=3, column=1) 

root.mainloop() 

這是一個較大的項目的一個片段,有爲了簡單起見,去掉generate()其餘功能。通過上面的代碼,可以創建一個窗口,其中包含三個標籤,三個相應的輸入字段和一個按鈕。生成按鈕旨在調用功能generate(),該功能依次創建基於其他代碼的地圖對象,然後調用root.quit()調用Tkinter的root.quit()會導致內核和窗口停止響應

然而,當按下按鈕時,窗口停止響應和蟒蛇內核反覆打印消息:

It seems the kernel died unexpectedly. Use 'Restart kernel' to continue using this console. 

我敢肯定,這是一個小的失誤,但如果有人能告訴我我哪裏錯我會很感激。

+0

只是爲了確認,即使這是「來自大型項目的片段」,您是說這個確切的代碼會導致您描述的行爲?你用什麼來運行它 - 從命令行運行時是否發生這種情況(例如:python the_script.py)? –

+0

以下幫助你嗎? https://github.com/spyder-ide/spyder/issues/3114 –

+0

@Bryan是的 - 運行這個確切的代碼會導致所描述的問題。然而,我用'root.destroy()'替換了'root.quit()',這似乎解決了這個問題。 –

回答

0

使用root.destroy()而不是root.quit()

root.quit()從我的理解,當你有你的主循環後,你想運行其他代碼,這是更好地利用。

"""Quit the Tcl interpreter. All widgets will be destroyed.""" 

root.destroy()將破壞您的主循環內的一切。並應該用於退出程序。

這是tkinter庫中方法的引用。

"""Destroy this and all descendants widgets. This will 
     end the application of this Tcl interpreter.""" 
相關問題