Override Alt-f4 Closing Tkinter Window In Python 3.6 And Replace It With Something Else
I spent a long time reading through past posts and didn't find quite what I need. I'm making a kiosk-type window using tkinter. I set it to open fullscreen. I want to override the
Solution 1:
There is a way to hook the Alt+F4 or pressing the X or any other way as far as I know:
root.protocol("WM_DELETE_WINDOW", do_exit)
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.
Here is an example:
from tkinter import *
root = Tk()
pressed_f4 = False# Is Alt-F4 pressed?defdo_exit():
global pressed_f4
print('Trying to close application')
if pressed_f4: # Deny if Alt-F4 is pressedprint('Denied!')
pressed_f4 = False# Reset variableelse:
close() # Exit applicationdefalt_f4(event): # Alt-F4 is pressedglobal pressed_f4
print('Alt-F4 pressed')
pressed_f4 = Truedefclose(*event): # Exit application
root.destroy()
root.bind('<Alt-F4>', alt_f4)
root.bind('<Escape>', close)
root.protocol("WM_DELETE_WINDOW",do_exit)
root.mainloop()
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.
Post a Comment for "Override Alt-f4 Closing Tkinter Window In Python 3.6 And Replace It With Something Else"