Python TKInter破坏不起作用

时间:2012-12-12 11:50:10

标签: python user-interface tkinter destroy

我正在尝试使用self.win.destroy关闭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

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

1 个答案:

答案 0 :(得分: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())