2012-12-12 109 views
1

我正在嘗試關閉一個帶有self.win.destroy的TKInter窗口。Python TKInter破壞不起作用

bind荷蘭國際集團事件的按鈕下面的錯誤被拋出:

... 
Can't invoke "bind" command: application has been destroyed 
During handling the above exception, another exception occured: 
... 
Can't invoke "destroy" command: application has been destroyed 

如何綁定一個「關閉窗口」命令按鈕?

+3

這將有助於如果你也將發佈相關的代碼。 – sloth

回答

4

這樣做:

button['command'] = root_window.destroy # give it the function 
# when the button is pressed the call() is done 

不要這樣做:

button.bind('<Button-1>', root_window.destroy()) #() makes the call 

因爲

root_window.destroy() 

銷燬窗口button.bind被調用之前。

這也是錯誤的:但不破壞根窗口:

button.bind('<Button-1>', root_window.destroy) 

因爲

  • 按鈕不能用鍵盤
  • root_window.destroy(event)被調用,但root.destroy()觸發只需要一個論據。

這不也行:

button.bind('<Button-1>', lambda event: root_window.destroy()) 
+1

你應該更清楚地說明問題不是「綁定與命令」,而是「root_window.destroy()與root_window.destroy」。雖然定義命令是非常適合在按鈕上設置綁定的,但您仍然是正確的。 –