- Moving a listbox vertically invokes the callback handler registered with its yscroll
command option. In this script, the sbar.set built-in method adjusts a scroll bar
proportionally.
In other words, moving one automatically moves the other. It turns out that every
scrollable object in tkinter—Listbox, Entry, Text, and Canvas—has built-in yview and
xview methods to process incoming vertical and horizontal scroll callbacks, as well as
yscrollcommand and xscrollcommand options for specifying an associated scroll bar’s
callback handler to invoke. All scroll bars have a command option, to name an associated
widget’s handler to be called on moves. Internally, tkinter passes information to all of
these methods, and that information specifies their new position (e.g., “go 10 percent
down from the top”), but your scripts usually need never deal with that level of detail.
Because the scroll bar and listbox have been cross-linked in their option settings, mov-
ing the scroll bar automatically moves the list, and moving the list automatically moves
the scroll bar. To move the scroll bar, either drag the solid part or click on its arrows
or empty areas. To move the list, click on the list and either use your arrow keys or
move the mouse pointer above or below the listbox without releasing the mouse button.
In all cases, the list and scroll bar move in unison. Figure 9-15 shows the scene after
expanding the window and moving down a few entries in the list, one way or another.
Figure 9-15. scrolledlist in the middle
Packing Scroll Bars
Finally, remember that widgets packed last are always clipped first when a window is
shrunk. Because of that, it’s important to pack scroll bars in a display as soon as possible
so that they are the last to go when the window becomes too small for everything. You
can generally make do with less than complete listbox text, but the scroll bar is crucial
526 | Chapter 9: A tkinter Tour, Part 2