0

Details:

Python 3.12.3

The tkinter <FocusOut> event should be triggered when the window looses focus. This works just fine in normal windows, but when I do root.overrideredirect(True), the window just stays on top even if I click other windows... and doesn't seem to receive/loose focus either...

I use ArchLinux OS, and I have a compositor that makes unfocused windows a bit transparent, and it doesn't seem to recognize it either, like it was never there...


Question:

Is there a way to fix this without removing root.overrideredirect(True)? Or an alternative to it, I just want to remove the decorations and close it when I click outside?


Example code:

import tkinter as tk
from pyautogui import position


win_size: tuple = (100, 100)
win_top_left: tuple = position()
win_bottom_right: tuple = (win_top_left[0] + win_size[0], win_top_left[1] + win_size[1])


def destroy(*_) -> None:
    WIN.destroy()


WIN: tk.Tk = tk.Tk()
WIN.title("Widnow")
WIN.geometry(f"{win_size[0]}x{win_size[1]}+{win_top_left[0]}+{win_top_left[1]}")
WIN.overrideredirect(True)

WIN.protocol("WM_DELETE_WINDOW", WIN.destroy)

WIN.bind("<FocusOut>", destroy)     # doesn't trigger Unless I remove WIN.overrideredirect(True)
WIN.bind("<FocusIn>", destroy)      # same

button: tk.Button = tk.Button(WIN, text="destroy", command=destroy)
button.pack()

WIN.mainloop()

I tried root.attributes('-type', 'splash') and it successfully made the window go down when i click on another window, but still doesn't receive/loose focus...

Thanks

2
  • Since no-one has tried to answer this, I will give my 2 not so good ideas. 1 - you can try to use the <Map> and <Unmap> events instead. 2 - don't use overrideredirect, instead use something like this for linux or this for windows (they both directly tell the window manager to only remove the title bar without affecting anything else)
    – TheLizzard
    Commented Jun 15 at 23:47
  • <FocusOut> works fine. When the window is open and click other window, the event callback is executed. Using Python 3.12.4 in Windows 11. Note that I have removed binding on <FocusIn> because it will be triggered when the program starts and then destroy the window.
    – acw1668
    Commented Jun 17 at 11:36

0

Browse other questions tagged or ask your own question.