In various applications, tkinter widgets are required to be focused to make them active. Widgets can also grab focus and prevent the other events outside the bounds. To manage and give focus to a particular widget, we generally use the focus_set() method. It focuses the widget and makes them active until the termination of the program.
In the following example, we have created two widgets: an Entry widget and a text widget in the same window. By using the focus_set() method, we will activate the focus on the text widget.
#Import tkinter library from tkinter import * from PIL import Image,ImageTk #Create an instance of tkinter frame win = Tk() #Set the geometry win.geometry("750x250") #Create a Text WIdget text= Text(win, width= 30, height= 10) text.insert(INSERT, "Hello World!") #Activate the focus text.focus_set() text.pack() #Create an Entry Widget entry=Entry(win,width= 30) entry.pack() win.mainloop()
Running the above code will display a window that contains the focus on the text widget.