1

Im trying to make this window fullscreen, then be able to escape with any key. I can't bind the escape key to actually escaping the window, can't figure out where it's going wrong from other similar posts too.

Here is the code:

import tkinter as tk
from tkinter import *

root=Tk()

root.overrideredirect(True)
root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))
root.focus_set()
root.bind("<Escape>", lambda e: root.quit())

#mybuttonsandframesgohere

root.mainloop()
Jkind9
  • 742
  • 7
  • 24

1 Answers1

1

On some systems (like Linux) root.overrideredirect(True) removes window manages (WM) which display border but WM also sends key/mouse events from system to your program. If WM doesn't send event then your program doesn't know that you clicked ESC.

This works for me on Linux Mint (based on Ubuntu and Debian). Rasbian is base on Debian.

import tkinter as tk

def close_escape(event=None):
    print("escaped")
    root.destroy()

root = tk.Tk()

root.overrideredirect(True)
root.overrideredirect(False)

root.attributes("-fullscreen", True)
root.wm_attributes("-topmost", 1)
root.focus_set()

root.bind("<Escape>", close_escape)

root.after(5000, root.destroy) # close after 5s if `ESC` will not work

root.mainloop()

I put root.after(5000, root.destroy) only for test - to close it after 5s if ESC will not work.

I use close_escape only to see if it was closed by ESC or after(). If code works then you can use root.bind("<Escape>", lambda event:root.destroy())

import tkinter as tk

root = tk.Tk()

root.overrideredirect(True)
root.overrideredirect(False)

root.attributes("-fullscreen", True)
root.wm_attributes("-topmost", 1)
root.focus_set()

root.bind("<Escape>", lambda event:root.destroy())

root.mainloop()

BTW: you may try also without root.overrideredirect() - maybe it will works for you

import tkinter as tk

root = tk.Tk()

root.attributes("-fullscreen", True)
root.wm_attributes("-topmost", 1) # keep on top
#root.focus_set()

root.bind("<Escape>", lambda event:root.destroy())

root.mainloop()
furas
  • 134,197
  • 12
  • 106
  • 148
  • It didn't close on command, but it did close after 5s, but also the print statements were printed out too. – Jkind9 Nov 27 '17 at 11:55
  • if it printed `"escaped"` then it should means it closed window with `ESC`. – furas Nov 27 '17 at 11:56
  • one thing to note: Replacing the function close_escape with root.destroy in root.bind does seem to work. Unsure why, but I will just keep the function in there – Jkind9 Nov 27 '17 at 12:01
  • I don't understand what is the use of doing `root.overrideredirect(True)` then immediately after `root.overrideredirect(False)`. For me it is the same as not using `overrideredirect` at all. – j_4321 Nov 27 '17 at 12:14
  • Why do you set the flag to `True` then to `False`? This does not make sense. – Billal Begueradj Nov 27 '17 at 12:42
  • @BillalBEGUERADJ @j_4321 today I see it works for me without `overrideredirect` but previously it needed `root.overrideredirect(True)` to toggle fullscreen. But `root.overrideredirect(True)` turned off events and `bind()` didn't worked, but if you set back `root.overrideredirect(False)` then fullscreen worked and events worked too. Strange situation. – furas Nov 27 '17 at 12:58
  • @j_4321 you are right `bind` sends `event` so it needs `lambda event:root.destroy()`. Corrected in answer. – furas Nov 27 '17 at 13:08
  • It is like writing +1 - 1, so those 2 lines are useless – Billal Begueradj Nov 27 '17 at 14:05
  • @BillalBEGUERADJ see my previouse comment - `True` and later `False` looks useless but on some systems (Linux) it was needed to corectly run `fullscreen` and `events`. It seems there is/was issue in `overrideredirect`. Today I see it works on my Linux even without `overrideredirect` but maybe other systems (other linux distributions) still have this problem. – furas Nov 27 '17 at 14:14
  • I never had a problem with using `widnow.attributes("-fullscreen", True)` on any Windows or Linux distro I used in the past. The problem here is related to combining `geometry()` manager and the `overrideredirect` flag and I believe your post, even if accepted, does not answer the question because: 1. You avoided to face the real problem which I just mentioned and 2. You misused, from 2 perspectives -including the justification pointed by your comment-, the use of that flag. – Billal Begueradj Nov 27 '17 at 14:40