When a GUI has more than one widget, a given widget can come under focus by an explicit mouse-click on the widget. Alternatively, the focus can be shifted to another given widget by pressing the Tab key on the keyboard in the order the widgets were created in the program.
It is therefore vital to create widgets in the order we want the user to traverse through them, or else the user will have a tough time navigating between the widgets using the keyboard.
Different widgets are designed to behave differently to different keyboard strokes. Let's therefore spend some time trying to understand the rules of traversing through widgets using the keyboard.
Let's look at the code of the 8.02 widget traversal.py
Python file to understand the keyboard traversal behavior for different widgets. Once you run the mentioned .py
file, it shows a window something like the following:
The code is simple. It adds an entry widget, a few buttons, a few radio buttons, a text widget, and a scale widget. However, it also demonstrates some of the most important keyboard traversal behaviors for these widgets.
Here are some important points to note (refer to 8.02 widget traversal.py
):
highlightthickness
option to a non-zero Integer
value for these widgets.highlightcolor= 'red'
in our code.takefocus = 1
option. You can explicitly exclude a widget from the tab navigation path by setting the takefocus= 0
option.takefocus = 0
) followed by all its children widgets.widget.focus_force()
to force the input focus to the widget.3.133.123.34