1

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 standard alt-f4 command to close it. I don't want it completely uncloseable though. I'd like to instead make up my own keyboard shortcut (alt-ctrl-g-3 or something like that).

I tried some of the suggestions for overriding alt-f4 and couldn't get them to work. In my init function I included:

self.bind('<Alt-Key-F4>', self.ignorekey())
.
.
.
def self.ignorekey(self)
    pass
Eric Oswald
  • 47
  • 1
  • 7
  • Can you post what methods you've tried? It could also be that your OS is still capturing the keystroke and sending the signal. – ahota Nov 18 '18 at 04:55
  • as far as capturing the alt-f4 that's the only method I tried since as far as I know that's the proper way to do it. I tried different identifiers for alt-f4 that I saw come up in various posts. I should mention that I'm using Ubuntu 18. – Eric Oswald Nov 18 '18 at 05:29

1 Answers1

4

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?

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

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.

figbeam
  • 7,001
  • 2
  • 12
  • 18
  • hm, but wouldn't that stop the window from closing in every situation? I was hoping to be able to create a special shortcut key combo that would be the only thing that closes the window. Basically I'm making this for a public kiosk and I don't want people to be able to use any standard keyboard shortcut or mouse click to be able to get out of the program. – Eric Oswald Nov 18 '18 at 18:35
  • You can bind any key to a function that will close the window. So make another function which closes the window and bind something to it. Or am I misunderstanding your question? – figbeam Nov 18 '18 at 19:16
  • Ah OK, maybe I misunderstood what this does. I thought it was setting a property of the window saying anytime it closed. My main problem at the moment is actually binding the alt-f4 keypress. When I run this code it instantly shows the messagebox saying alt-f4 was pressed even though it wasn't and when I do hit alt-f4 it closes the window rather than executing the function: class Application(Frame): def __init__(self, master): self.bind('', self.ignorekey()) def ignorekey(self): messagebox.showinfo("Invalid Email Address", "You hit alt-f4") – Eric Oswald Nov 18 '18 at 22:22
  • Didn't quite understand that... I'm including an example of usage in my answer. – figbeam Nov 19 '18 at 00:03
  • What do you mean? – figbeam Nov 19 '18 at 10:10
  • Oh I see, sorry, was looking at it on mobile and didn't see it. Ill give that a try. – Eric Oswald Nov 19 '18 at 18:00
  • OK, thanks, yeah I tried that code you added and alt-f4 still closed the window. – Eric Oswald Nov 20 '18 at 20:16