2017-10-04 169 views
-1

使用.bind方法時,窗口是否丟失了焦點並且可以綁定到tkinter窗口,是否有某些事件?Tkinter - 窗口丟失焦點事件

+0

看看http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/event-types.html –

+2

的可能的複製[TK/Tkinter的:檢測應用程序失去焦點]( https://stackoverflow.com/questions/18089068/tk-tkinter-detect-application-lost-focus) – Dmitry

回答

2

您正在尋找的活動是<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() 
+0

我正在使用'window.overrideredirect(True)',並且這不起作用。 –

+0

@JakubBláha:您需要在您的問題中添加該細節。 –