检测何时按下“x”或关闭按钮

Jam*_*ala 3 python tkinter python-3.x

您如何知道用户何时在 python tkinter 程序中按下关闭按钮上的“x”按钮并执行事件?

Pyt*_*sta 5

您可以覆盖关闭协议。

def on_close():

     #custom close options, here's one example:

     close = messagebox.askokcancel("Close", "Would you like to close the program?")
     if close:
          root.destroy()

root.protocol("WM_DELETE_WINDOW",  on_close)
Run Code Online (Sandbox Code Playgroud)