【问题标题】:tkinter Text <Destroy> event comes too late <solved>tkinter Text <Destroy> 事件来得太晚了 <solved>
【发布时间】:2020-12-16 15:56:47
【问题描述】:

我有一个脚本,它使用 Text 小部件来编辑文本文件。 当窗口关闭时(单击“关闭”图标),我想检查修改后的文件是否未保存。

我已经绑定了 Destroy 事件

`self.bind('<Destroy>', self.end_act)`

但是 end_act() 失败了

`_tkinter.TclError: invalid command name ".!textwidget"`

表示小部件已经关闭。

还有其他事件可以达到这个结果吗?

【问题讨论】:

标签: tkinter widget bind


【解决方案1】:

改用self.protocol("WM_DELETE_WINDOW", ...)

    self.protocol("WM_DELETE_WINDOW", self.on_close)

...

def on_close(self):
    self.end_act() # execute the required function you want
    self.destroy() # then close the window

【讨论】:

  • 谢谢,这正是我所需要的。之前没用过.protocol
猜你喜欢
  • 2012-05-17
  • 1970-01-01
  • 2011-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多