我正在尝试使用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
如何将“关闭窗口”命令绑定到按钮?
答案 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())