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
|
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 —
|
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