How do you check if a widget has focus in Tkinter?
问题 from Tkinter import * app = Tk() text_field = Entry(app) text_field.pack() app.mainloop() I want to be able to check if text_field is currently selected or focused, so that I know whether or not to do something with its contents when the user presses enter. 回答1: If you want to do something when the user presses enter only if the focus is on the entry widget, simply add a binding to the entry widget. It will only fire if that widget has focus. For example: import tkinter as tk root = tk.Tk()