focus_set() method-

This method is used to set the focus on the desired widget if and only if the master window is focused.

Syntax:

widget.focus_set()

Below is the Python program-




# Importing tkinter module
# and all functions
from tkinter import * 
from tkinter.ttk import *
  
# creating master window
master = Tk()
  
# Entry widget
e1 = Entry(master)
e1.pack(expand = 1, fill = BOTH)
  
# Button widget which currently has the focus
e2 = Button(master, text ="Button")
  
# here focus_set() method is used to set the focus
e2.focus_set()
e2.pack(pady = 5)
  
# Radiobuton widget
e3 = Radiobutton(master, text ="Hello")
e3.pack(pady = 5)
  
# Infinite loop
mainloop()


Output:

You may observe in above image that Button widget has the focus. For better understanding copy and run above program.

Python | focus_set() and focus_get() method

Tkinter has a number of widgets to provide functionality in any GUI. It also supports a variety of universal widget methods which can be applied on any of the widget.
focus_get() and focus_set() methods are also universal widget methods. They can also be applied on Tk() method.

Similar Reads

focus_set() method-

This method is used to set the focus on the desired widget if and only if the master window is focused....

focus_get() method-

This method returns the name of the widget which currently has the focus....