Disable focus for tkinter widgets – Python
In this article, we will discuss how to disable the focus from the widgets in the Tkinter framework. For disabling the focus, we use takefocus option inside the widget and assign its value to 0.
Step1: Import Tkinter and initialize tkinter window
Step 2: Add some widgets to the application
# Creating button widget
btn = tk.Button(root, text=”Button”) # initializing the widget.
btn.pack() # calling the widget in application
Here we notice that we are getting focus on each widget after pressing the TAB key.
Step-3: Adding disable focus functionality to our program.
We use takefocus argument for disabling the focus
Syntax: takefocus = 0 # for button btn = tk.Button(root, text="Button", takefocus=0)
The approach of the program is to disable the focus from the widgets with the help takefocus argument. We put its value to 0 for doing so. Other widgets have their focus enabled.
Red-colored widgets have takefocus=0 and others don’t have this argument. The red color is only used for clarity you may also remove it.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course