python - Tkinter - 窗口焦点丢失事件

标签 python tkinter window focus

tkinter 窗口失去焦点时是否有一些事件触发可以使用 .bind 方法绑定(bind)到 tkinter 窗口?

最佳答案

您要查找的事件是<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()

关于python - Tkinter - 窗口焦点丢失事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46567324/

相关文章:

Python:替换重音符号(é 到 e)、删除 [^a-zA-Z\d\s] 和 lower() 的有效方法

python - 有没有办法让 tkinter 在打开时立即执行命令?

python - 在 python 中的 tkinter.ttk 中使图像精确居中

wpf - Window.SourceInitialized 事件何时引发

java - 仅创建一个小程序实例并更新其余实例

python - PyQt 通过单击另一个小部件上的按钮来显示一个小部件

python - 连接到套接字的 Python 线程的理想 stack_size 是多少?

python - 打印按值排序的字典

python - tkinter - 将 for 循环中的按钮链接到函数字典 {name : function}

java - 父对话框状态更改为不可见,但子对话框仍然保持可见