1

我正在尝试关闭 TKInter 窗口self.win.destroy

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

如何将“关闭窗口”命令绑定到按钮?

4

1 回答 1

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())
于 2012-12-12T13:29:28.503 回答