Widget traversal

If a GUI has more than one widget, a given widget can come under focus when you explicitly click on the widget. Alternatively, the focus can be shifted to other widgets in the order that the widgets were created in the program by pressing the Tab key on the keyboard.

Therefore, it is vital to create widgets in the order that we want the user to traverse through them. Otherwise, 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. Therefore, let's spend some time trying to understand the rules of traversing through widgets using the keyboard.

Have a look at the 10.02_widget_traversal.py file to understand the keyboard traversal behavior for different widgets. The code displays a window like the one shown in the following screenshot:

The code will not be given here as it is very simple (see the 10.02_widget_traversal.py code). It simply adds an entry widget, a few buttons, a few radio buttons, a text widget, a label widget, and a scale widget.

The code demonstrates the default keyboard traversal behaviors for these widgets in Tkinter.

The following are a few important points that you should note:

  • The Tab key can be used to traverse forward, and Shift + Tab can be used to traverse backward.
  • A user can traverse through the widgets in the order that they were created. A parent widget is visited first (unless it is excluded using takefocus = 0), followed by all of its children widgets.
  • You can use widget.focus_force() to force the input focus on a widget. 
  • You cannot traverse a text widget by using the Tab key because the text widget can contain tab characters as its content. Instead, the text widget can be traversed by using Ctrl + Tab.
  • Buttons on the widget can be pressed using the spacebar. Similarly, check buttons and radio buttons can also be traversed using the spacebar.
  • You can go up and down the items in a Listbox widget by using the up and down arrows.
  • The Scale widget responds to the left and right arrow keys and the up and down arrow keys. Similarly, the Scrollbar widget responds to the left/right or up/down arrow keys, depending on their orientation.
  • By default, most of the widgets (except Frame, Label, and Menus) get an outline when they have the focus set on them. This outline is normally displayed as a thin black border around the widget. You can even set the Frame and Label widgets to show this outline by setting the highlightthickness option to a nonzero integer value for these widgets.
  • We can change the color of the outline by using highlightcolor= 'red' in the code.
  • Frame, Label, and Menu are not included in the tab's navigation path. However, they can be included in the navigation path by using the takefocus = 1 option. You can explicitly exclude a widget from the tab's navigation path by using the takefocus = 0 option.

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.26.138