2012-12-12 106 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”。虽然定义命令是非常适合在按钮上设置绑定的,但您仍然是正确的。 –