2

I'm building a Tkinter app that will ask users to set a password before using the computer. Therefore, it needs to be unclosable, which includes disabling Alt+F4.

I have tried using root.protocol("WM_DELETE_WINDOW", preventClose) where preventClose is a function (shown below). Note: This is not a duplicate of override alt-f4 closing tkinter window in python 3.6 and replace it with something else. I want a completely unclosable window, not just to remap Alt+F4.

This is the method I've tried using the preventClose function:

def preventClose():
    pass

and this Tkinter protocol:

root.protocol("WM_DELETE_WINDOW", preventClose)

This has not been able to disable Alt+F4 like described in Unclosable window using tkinter.

I do not see any error messages, but Alt+F4 is not disabled like I want it to be.

Some basic information:

  • Windows 10 Home 64-bit
  • Python 3.7
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61

1 Answers1

1

woop okay found something on a diff question here

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 application

def alt_f4(event):  # Alt-F4 is pressed
    global pressed_f4
    print('Alt-F4 pressed')
    pressed_f4 = True

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