Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter - window focus loss event

Is there some event triggering when tkinter window loses focus that can be bound to a tkinter window using the .bind method?

like image 844
Jakub Bláha Avatar asked Oct 04 '17 14:10

Jakub Bláha


1 Answers

The event you are looking for is <FocusOut>.

import tkinter as tk

def on_focus_out(event):
    if event.widget == root:
        label.configure(text="I DON'T have focus")

def on_focus_in(event):
    if event.widget == root:
        label.configure(text="I have focus")

root = tk.Tk()
label = tk.Label(width=30)
label.pack(side="top", fill="both", expand=True)

root.bind("<FocusIn>", on_focus_in)
root.bind("<FocusOut>", on_focus_out)

root.mainloop()
like image 199
Bryan Oakley Avatar answered Oct 21 '22 06:10

Bryan Oakley