Setting the focus to a specific Tkinter entry widget

Tkinter has many universal methods that add functionalities to widgets and elements. In order to set the focus to a particular widget, we have the focus_set() method which is used to focus a specific widget in a group of widgets present in an application.


In this example, we have created numeric keys in the range 0-9. We have set the focus for Numeric key ‘2’.

#Import the required libraries
from tkinter import *
import webbrowser
#Create an instance of tkinter frame
win = Tk()
#Define function for different operations
def close():
#Create a Label widget
text=Label(win, text="", font=('Helvetica bold ',25)) 110, y= 50,)
#Create Button widgets
b1=Button(win, text= "1"), y=20)
b2=Button(win, text= "2"), y=20)
b3=Button(win, text= "3"), y=20)
b4=Button(win, text= "4"), y=50)
b5=Button(win, text= "5"), y=50)
b6=Button(win, text= "6"), y=50)
b7=Button(win, text= "7"), y=80)
b8=Button(win, text= "8"), y=80)
b9=Button(win, text= "9"), y=80)
b0=Button(win, text= "0"), y=80)
#Set the focus on button b1
b2=Button(win, text= "Close", command= close),y= 140)


Running the above code will display the focus for numeric key ‘2’.

To change the focus of the widget, just add the focus_set.() method to the widget you want to set focus on.

Updated on: 16-Apr-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started