gpt4 book ai didi

Python TKInter 破坏不工作

转载 作者:太空宇宙 更新时间:2023-11-04 10:51:01 24 4
gpt4 key购买 nike

我正在尝试使用 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

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

最佳答案

这样做:

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())

关于Python TKInter 破坏不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13839123/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com