Change language

Python | focus_set () and focus_get () method

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

focus_set () method

This method is used to set focus to the desired widget if and only if, when the main window is focused.

Syntax :

 widget.focus_set () 

Below is a Python program

# Import tkinter module
# and all functions

from tkinter import *  

from tkinter.ttk import *

 
# create main window

master =   Tk ()

 
# Login widget

e1 = Entry (master)

e1.pack (expand = 1 , fill = BOTH)

 
# The button widget that currently has focus

e2 = Button (master, text = "Button" )

  
# here the focus_set () method is used to set focus
e2.focus_set ()

e2.pack (pady = 5 )

 
# Radiobutton widget

e3 = Radiobutton (master, text = "Hello" )

e3 .pack (pady = 5 )

 
# Endless loop
mainloop ( )

Output:

You can see in the image above that the button widget has focus.  For a better understanding, copy and run the above program .

focus_get () method

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

Syntax :

 master.focus_get () 

Note: you can use it with any widget, master not needed.

Below is a Python program —

# Import tkinter module
# and all functions

from tkinter import *  

from tkinter.ttk import *

 
# create chapters window

master = Tk ()

 
# This method is used to get
# widget name
# which currently has focus
# clicking mouse-1

def focus (event):

widget = master.focus_get ()

print (widget, "has focus" )

 
# Login 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 the focus () function is associated with the Button-1 mouse
# so every time you click it will call
# focus method defined above

master.bind_all ( ""Button-1"" , lambda e: focus (e))

 
# endless loop
mainloop ()

Output: Every time you click on any widget OR if you click the mouse-1 above, the program will remind chat the name of the widget that has focus. For a better understanding, copy and run the above program.

.! Radiobutton has focus.! Entry has focus.! Button has focus 

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically