There is a way to hook the or pressing the X or any other way as far as I know: root.protocol("WM_DELETE_WINDOW", do_exit)其中 do_exit() 是回调函数,root 是您的主窗口.where do_exit() is the callback function and root is your main window. 此绑定不传递事件对象.据我所知,这应该适用于任何平台.This binding does not pass an event object. As far as I can see this should work for any platform.这是一个例子:from tkinter import *root = Tk()pressed_f4 = False # Is Alt-F4 pressed?def do_exit(): global pressed_f4 print('Trying to close application') if pressed_f4: # Deny if Alt-F4 is pressed print('Denied!') pressed_f4 = False # Reset variable else: close() # Exit applicationdef alt_f4(event): # Alt-F4 is pressed global pressed_f4 print('Alt-F4 pressed') pressed_f4 = Truedef close(*event): # Exit application root.destroy()root.bind('<Alt-F4>', alt_f4)root.bind('<Escape>', close)root.protocol("WM_DELETE_WINDOW",do_exit)root.mainloop()我不确定来自 root.bind('<Alt-F4>', alt_f4) 的回调总是会在来自 root.protocol("WM_DELETE_WINDOW",do_exit).你可能需要做更多的研究来确定这一点.I'm not sure that the callback from root.bind('<Alt-F4>', alt_f4) always will run before the callback from root.protocol("WM_DELETE_WINDOW",do_exit). You may have to do more research to establish that. 这篇关于在python 3.6中覆盖alt-f4关闭tkinter窗口并将其替换为其他内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-27 09:44