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.

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-

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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()

chevron_right


Output:

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

focus_get() method-

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

Syntax:

master.focus_get()

Note: You can use it with any of the widget, master in not necessary.

Below is the Python program –

filter_none

edit
close

play_arrow

link
brightness_4
code

# Importing tkinter module
# and all functions
from tkinter import * 
from tkinter.ttk import *
  
# creating master window
master = Tk()
  
# This method is used to get
# the name of the widget
# which currently has the focus
# by clicking Mouse Button-1
def focus(event):
    widget = master.focus_get()
    print(widget, "has focus")
  
# Entry widget
e1 = Entry(master)
e1.pack(expand = 1, fill = BOTH)
  
# Button Widget
e2 = Button(master, text ="Button")
e2.pack(pady = 5)
  
# Radiobutton widget
e3 = Radiobutton(master, text ="Hello")
e3.pack(pady = 5)
  
# Here function focus() is binded with Mouse Button-1
# so every time you click mouse, it will call the
# focus method, defined above
master.bind_all("<Button-1>", lambda e: focus(e))
  
# infinite loop
mainloop()

chevron_right


Output: Every time you click on any widget OR if you click the mouse button-1 above program will print the name of the widget which has the focus. For better understanding copy and run above program.

.!radiobutton has focus
.!entry has focus
.!button has focus


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.