【问题标题】:Python TKInter destroy not workingPython TKInter破坏不起作用
【发布时间】:2012-11-30 02:52:15
【问题描述】:

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

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

【问题讨论】:

  • 如果您也发布相关代码会有所帮助。

标签: python user-interface tkinter destroy


【解决方案1】:

这样做:

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

【讨论】:

  • 您应该更清楚地表明问题不是“绑定与命令”,而是“root_window.destroy() 与 root_window.destroy”。尽管您是正确的,但定义命令比在按钮上设置绑定更可取。
猜你喜欢
  • 1970-01-01
  • 2015-06-09
  • 1970-01-01
  • 2015-04-02
  • 1970-01-01
  • 1970-01-01
  • 2015-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多