Next / Previous / Contents / TCC Help System / NM Tech homepage

16. The Scrollbar widget

A number of widgets, such as listboxes and canvases, can act like sliding windows into a larger virtual area. You can connect scrollbar widgets to them to give the user a way to slide the view around relative to the contents. Here's a screen shot of an entry widget with an associated scrollbar widget:

To create a new scrollbar widget as the child of a root window or frame master:

    w = Scrollbar ( master, option, ... )

The constructor returns the new scrollbar widget. Options for scrollbars include:

activebackgroundThe color of the slider and arrowheads when the mouse is over them. See Section 4.3, “Colors”.
bg or backgroundThe color of the slider and arrowheads when the mouse is not over them.
bd or borderwidthThe width of the 3-d borders around the entire perimeter of the trough, and also the width of the 3-d effects on the arrowheads and slider. Default is no border around the trough, and a 2-pixel border around the arrowheads and slider.
commandA procedure to be called whenever the scrollbar is moved. For a discussion of the calling sequence, see Section 16.1, “The scrollbar command callback”.
cursorThe cursor that appears when the mouse is over the scrollbar. See Section 4.8, “Cursors”.
elementborderwidthThe width of the borders around the arrowheads and slider. The default is elementborderwidth=-1, which means to use the value of the borderwidth option.
highlightbackgroundThe color of the focus highlight when the scrollbar does not have focus. See Section 23, “Focus: routing keyboard input”.
highlightcolorThe color of the focus highlight when the scrollbar has the focus.
highlightthicknessThe thickness of the focus highlight. Default is 1. Set to 0 to suppress display of the focus highlight.
jumpThis option controls what happens when a user drags the slider. Normally (jump=0), every small drag of the slider causes the command callback to be called. If you set this option to 1, the callback isn't called until the user releases the mouse button.
orientSet orient=HORIZONTAL for a horizontal scrollbar, orient=VERTICAL for a vertical one.
repeatdelayThis option controls how long button 1 has to be held down in the trough before the slider starts moving in that direction repeatedly. Default is repeatdelay=300, and the units are milliseconds.
repeatintervalThis option controls how often slider movement will repeat when button 1 is held down in the trough. Default is repeatinterval=100, and the units are milliseconds.
takefocusNormally, you can tab the focus through a scrollbar widget; see Section 23, “Focus: routing keyboard input”. Set takefocus=0 if you don't want this behavior. The default key bindings for scrollbars allow the user to use the ← and → arrow keys to move horizontal scrollbars, and they can use the ↑ and ↓ keys to move vertical scrollbars.
troughcolorThe color of the trough.
widthWidth of the scrollbar (its y dimension if horizontal, and its x dimension if vertical). Default is 16. For possible values, see Section 4.1, “Dimensions”.

Methods on scrollbar objects include:

.get()

Returns two numbers (a, b) describing the current position of the slider. The a value gives the position of the left or top edge of the slider, for horizontal and vertical scrollbars respectively; the b value gives the position of the right or bottom edge. Each value is in the interval [0.0, 1.0] where 0.0 is the leftmost or top position and 1.0 is the rightmost or bottom position. For example, if the slider extends from halfway to three-quarters of the way along the trough, you might get back the tuple (0.5,0.75).

.set ( first, last )

To connect a scrollbar to another widget w, set w's xscrollcommand or yscrollcommand to the scrollbar's .set method. The arguments have the same meaning as the values returned by the .get() method. Please note that moving the scrollbar's slider does not move the corresponding widget.