This chapter describes the Tk scrollbar
. Scrollbars have a general protocol that is used to attach them to one or more other widgets.
Scrollbars control other widgets through a standard protocol based around Tcl commands. A scrollbar uses a Tcl command to ask a widget to display part of its contents. The scrollable widget uses a Tcl command to tell the scrollbar what part of its contents are visible. The Tk widgets designed to work with scrollbars are: entry, listbox, text, and canvas. The scrollbar protocol is general enough to use with new widgets, or collections of widgets. This chapter explains the protocol between scrollbars and the widgets they control, but you don't need to know the details to use a scrollbar. All you need to know is how to set things up, and then these widgets take care of themselves.
The following commands create a text widget and two scrollbars that scroll it horizontally and vertically:
scrollbar .yscroll -command {.text yview} -orient vertical scrollbar .xscroll -command {.text xview} -orient horizontal text .text -yscrollcommand {.yscroll set} -xscrollcommand {.xscroll set}
The scrollbar's set
operation is designed to be called from other widgets when their display changes. The scrollable widget's xview
and yview
operations are designed to be called by the scrollbar when the user manipulates them. Additional parameters are passed to these operations as described later. In most cases you can ignore the details of the protocol and just set up the connection between the scrollbar and the widget.
Example 33-1. A text widget and two scrollbars
proc Scrolled_Text { f args } { frame $f eval {text $f.text -wrap none -xscrollcommand [list $f.xscroll set] -yscrollcommand [list $f.yscroll set]} $args scrollbar $f.xscroll -orient horizontal -command [list $f.text xview] scrollbar $f.yscroll -orient vertical -command [list $f.text yview] grid $f.text $f.yscroll -sticky news grid $f.xscroll -sticky news grid rowconfigure $f 0 -weight 1 grid columnconfigure $f 0 -weight 1 return $f.text } set t [Scrolled_Text .f -width 40 -height 8 -font {courier 12}] pack .f -side top -fill both -expand true set in [open [file join $tk_library demos colors.tcl]] $t insert end [read $in] close $in
Example 33-1 defines Scrolled_Text
that creates a text widget with two scrollbars. It reads and inserts one of the Tk demo files into the text widget. There is not enough room to display all the text, and the scrollbars indicate how much text is visible. Chapter 36 describes the text widget in more detail.
The list
command constructs the -command
and -xscrollcommand
values. Even though one could use double quotes here, you should make a habit of using list
when constructing values that are used later as Tcl commands. Example 33-1 uses args
to pass through extra options to the text widget. The use of eval
and args
is explained in Example 10-3 on page 136. The scrollbars and the text widget are lined up with the grid
geometry manager as explained in Example 26-10 on page 417.
When the user manipulates the scrollbar, it calls its registered command
with some additional parameters that indicate what the user said to do. The associated widget responds to this command (e.g., its xview
operation) by changing its display. After the widget changes its display, it calls the scrollbar by using its registered xscrollcommand
or yscrollcommand
(e.g., the set
operation) with some parameters that indicate the new relative size and position of the display. The scrollbar updates its appearance to reflect this information.
The protocol supports widgets that change their display by themselves, such as when more information is added to the widget. Scrollable widgets also support a binding to <B2-Motion>
(i.e., "middle drag") that scrolls the widget. When anything happens to change the view on a widget, the scrollable widgets use their scroll commands to update the scrollbar.
The scrollbar set
operation takes two floating point values between zero and one, first
and last
, that indicate the relative position of the top and bottom (or left and right) of the widget's display. The scrollable widget adds these values when they use their yscrollcommand
or xscrollcommand
. For example, the text widget would issue the following command to indicate that the first quarter of the widget is displayed:
.yscroll set 0.0 0.25
If the two values are 0.0 and 1.0, it means that the widget's contents are fully visible, and a scrollbar is not necessary. You can monitor the protocol by using a Tcl wrapper, Scroll_Set
, instead of the set
operation directly. Scroll_Set
waits for the scrollbar to be necessary before mapping it with a geometry manager command. It is not safe to unmap the scrollbar because that can change the size of the widget and create the need for a scrollbar. That leads to an infinite loop.
Example 33-2. Scroll_Set
manages optional scrollbars
proc Scroll_Set {scrollbar geoCmd offset size} { if {$offset != 0.0 || $size != 1.0} { eval $geoCmd ;# Make sure it is visible } $scrollbar set $offset $size }
Scroll_Set
takes a geometry management command as an argument, which it uses to make the scrollbar visible. Example 33-3 uses Scroll_Set
with a listbox. Note that it does not grid the scrollbars directly. Instead, it lets Scroll_Set
do the geometry command the first time it is necessary.
Example 33-3. Listbox with optional scrollbars
proc Scrolled_Listbox { f args } { frame $f listbox $f.list -xscrollcommand [list Scroll_Set $f.xscroll [list grid $f.xscroll -row 1 -column 0 -sticky we]] -yscrollcommand [list Scroll_Set $f.yscroll [list grid $f.yscroll -row 0 -column 1 -sticky ns]] eval {$f.list configure} $args scrollbar $f.xscroll -orient horizontal -command [list $f.list xview] scrollbar $f.yscroll -orient vertical -command [list $f.list yview] grid $f.list -sticky news grid rowconfigure $f 0 -weight 1 grid columnconfigure $f 0 -weight 1 return $f.list } set l [Scrolled_Listbox .f -listvariable fonts] pack .f -expand yes -fill both set fonts [lsort -dictionary [font families]]
Scrolled_Listbox
takes optional parameters for the listbox. It uses eval
to configure the listbox with these arguments. The style of using eval
shown here is explained in Example 10-3 on page 136. Example 46-4 on page 686 associates two listboxes with one scrollbar.
The xview
and yview
operations are designed to be called from scrollbars, and they work the same for all scrollable widgets. You can use them to scroll the widgets for any reason, not just when the scrollbar is used. The following examples use a text widget named .text
for illustration.
The xview
and yview
operations return the current first
and last
values that would be passed to a scrollbar set
command:
.text yview
=> 0.2 0.55
When the user clicks on the arrows at either end of the scrollbar, the scrollbar adds scroll
num
units
to its command, where num
is positive to scroll down, and negative to scroll up. Scrolling up one line is indicated with this command:
.text yview scroll -1 units
When the user clicks above or below the elevator of the scrollbar, the scrollbar adds scroll
num
pages
to its command. Scrolling down one page is indicated with this command:
.text yview scroll 1 pages
You can position a widget so that the top (or left) edge is at a particular offset from the beginning of the widget's contents. The offset is expressed as a floating point value between zero and one. To view the beginning of the contents:
.text yview moveto 0.0
If the offset is 1.0, the last part of the widget content's is displayed. The Tk widgets always keep the end of the widget contents at the bottom (or right) edge of the widget, unless the widget is larger than necessary to display all the contents. You can exploit this with the one-line entry widget to view the end of long strings:
.entry xview moveto 1.0
Tk 8.0 uses native scrollbar widgets on Macintosh and Windows. While the use of scrollbars with other widgets is identical on all platforms, the interpretation of the attributes and the details of the bindings vary across platforms. This section describes the Tk scrollbar on UNIX. The default bindings and attributes are fine on all platforms, so the differences should not be important.
The scrollbar is made up of five components: arrow1
, trough1
, slider
, trough2
, and arrow2
. The arrows are on either end, with arrow1
being the arrow to the left for horizontal scrollbars, or the arrow on top for vertical scrollbars. The slider represents the relative position of the information displayed in the associated widget, and the size of the slider represents the relative amount of the information displayed. The two trough regions are the areas between the slider and the arrows. If the slider covers all of the trough area, you can see all the information in the associated widget.
Table 33-1 lists the default bindings for scrollbars on UNIX. Button 1 and button 2 of the mouse have the same bindings. You must direct focus to a scrollbar explicitly for the key bindings like <Up>
and <Down>
to take effect.
Table 33-1. Bindings for the scrollbar widget
| Clicking on the arrows scrolls by one unit. Clicking on the trough moves by one screenful. |
| Dragging the slider scrolls dynamically. |
| Clicking on the trough or arrow scrolls all the way to the beginning (end) of the widget. |
| Scrolls up (down) by one unit. |
| Scrolls up (down) by one screenful. |
| Scrolls left (right) by one unit. |
| Scrolls left (right) by one screenful. |
| Scrolls back (forward) by one screenful. |
| Scrolls all the way to the left (top). |
| Scrolls all the way to the right (bottom). |
Table 33-2 lists the scrollbar attributes. The table uses the resource name for the attribute, which has capitals at internal word boundaries. In Tcl commands, the attributes are specified with a dash and all lowercase.
There is no length
attribute for a scrollbar. Instead, a scrollbar is designed to be packed next to another widget with a fill option that lets the scrollbar
display grow to the right size. Only the relief of the active element can be set. The background
color is used for the slider, the arrows, and the border. The slider and arrows are displayed in the activeBackground
color when the mouse is over them. The trough is always displayed in the troughColor
.
Table 33-2. Attributes for the scrollbar widget
| Color when the mouse is over the slider or arrows. |
| Relief of slider and arrows when mouse is over them. |
| The background color (also |
| Extra space around the edge of the scrollbar. |
| Prefix of the command to invoke when the scrollbar changes. Typically this is a |
| Cursor to display when mouse is over the widget. |
| Border width of arrow and slider elements. |
| Focus highlight color when widget does not have focus. |
| Focus highlight color when widget has focus. |
| Thickness of focus highlight rectangle. |
| Width of 3D border on arrows and slider. |
| If true, dragging the elevator does not scroll dynamically. Instead, the display jumps to the new position. |
| Orientation: |
| Milliseconds before auto-repeat starts. Auto-repeat is used when pressing |
| Milliseconds between auto-repeat events. |
| The color of the bar on which the slider sits. |
| Width of the narrow dimension of the scrollbar. |
The scrollbar operations are primarily used by the default bindings. Table 33-3 lists the operations supported by the scrollbar. In the table, $w
is a scrollbar widget.
Table 33-3. Operations on the scrollbar widget
| Queries or sets the active element, which can be |
| Returns the value of the configuration option. |
| Queries or modifies the widget configuration. |
| Returns the change in the first argument to |
| Returns a number between 0 and 1 that indicates the relative location of the point in the trough. |
| Returns first and last from the |
| Returns arrow1, trough1, slider, trough2, or arrow2, to indicate what is under the point. |
| Sets the scrollbar parameters. first is the relative position of the top (left) of the display. last is the relative position of the bottom (right) of the display. |
18.117.196.184