Skip to content
Advertisement

Cant exit fullscreen on tkinter window python

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

Advertisement

Answer

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()
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement