scrollbar
Guide: Scale and scrollbar
Makes a scrollbar: a bar with a slider that scrolls the view of another widget.
scrollbar PATHNAME ?OPTION VALUE ...?
Description
Makes a scrollbar widget with the name PATHNAME and the options of the call, and makes the command PATHNAME for the widget commands below. A scrollbar and a scrollable widget, such as a listbox or a text, work as a pair:
- The
-yscrollcommand(or-xscrollcommand) of the widget callssetof the scrollbar with the visible part of the content. The slider then shows that part. - The
-commandof the scrollbar callsyview(orxview) of the widget when the user drags the slider or clicks the trough.
The parent of PATHNAME must exist, and PATHNAME must not. A scrollbar is a Scrollbar in CSS and for the option database, and ztk::class gives it a style class for a stylesheet. The default -orient is vertical.
The modern scrollbar has a trough and a slider, and no arrows. The theme classic has the look of Tk on X11.
Widget commands
| Command | Description |
|---|---|
scrollbar activate | Gives or sets the active element of the scrollbar. |
scrollbar delta | Gives the change of the fraction for a move of the slider by a distance. |
scrollbar fraction | Gives the fraction of the trough at a point. |
scrollbar get | Gives the values of the last set. |
scrollbar identify | Gives the element of the scrollbar at a point. |
scrollbar set | Sets the visible part of the content, which the slider shows. |
scrollbar cget | Gives the value of an option of the scrollbar. |
scrollbar configure | Gives or sets the options of the scrollbar. |
Options
| Option | Type | Default | Database name | Description |
|---|---|---|---|---|
-activebackground | color | #ececec | activeBackground | The background color of the active state, in the look of Tk on X11. |
-activerelief | relief | raised | activeRelief | The relief of the active element. |
-background | color | #d9d9d9 | background | The background color of the scrollbar. |
-borderwidth | screen distance | 1 | borderWidth | The width of the border of the scrollbar. |
-command | script | empty | command | The command prefix that the scrollbar calls to scroll its widget. |
-cursor | cursor name | empty | cursor | The shape of the pointer over the scrollbar. |
-elementborderwidth | screen distance | -1 | elementBorderWidth | The width of the borders of the slider and the arrows. |
-highlightbackground | color | #d9d9d9 | highlightBackground | The color of the focus ring while the scrollbar has no focus, in the look of Tk on X11. |
-highlightcolor | color | #000000 | highlightColor | The color of the focus ring while the scrollbar has the focus, in the look of Tk on X11. |
-highlightthickness | screen distance | 0 | highlightThickness | The width of the focus ring, in the look of Tk on X11. |
-jump | boolean | 0 | jump | Whether a drag of the slider scrolls only at the release of the button. |
-orient | orientation | vertical | orient | The direction of the scrollbar: horizontal or vertical. |
-relief | relief | sunken | relief | The 3-D look of the border. |
-repeatdelay | integer | 300 | repeatDelay | The time in milliseconds before a held button starts to repeat. |
-repeatinterval | integer | 100 | repeatInterval | The time in milliseconds between two repeats of a held button. |
-takefocus | string | empty | takeFocus | Whether the Tab key can move the focus to the scrollbar. |
-troughcolor | color | #b3b3b3 | troughColor | The color of the trough, in the look of Tk on X11. |
-width | screen distance | 11 | width | The width of the short side of the scrollbar. |
-activebackground COLOR
The background color of the active state, in the look of Tk on X11.
The color of the slider while the pointer is over it. A color is a name such as red, or #RGB, #RRGGBB, or #RRRRGGGGBBBB.
% scrollbar .activebackground -activebackground {#336699}
> .activebackground cget -activebackground
#336699% scrollbar .activebackground2 -activebackground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-activerelief RELIEF
The relief of the active element.
In Tk, the element of activate has this 3-D look. ztk stores the value with no effect.
Values: flat, groove, raised, ridge, solid, sunken.
% scrollbar .activerelief -activerelief groove
> .activerelief cget -activerelief
groove% scrollbar .activerelief2 -activerelief bumpy
bad relief "bumpy": must be flat, groove, raised, ridge, solid, or sunken
-errorcode TCL LOOKUP INDEX relief bumpy-background COLOR
The background color of the scrollbar.
The color behind the content. -bg is the same option. A color is a name such as red, or #RGB, #RRGGBB, or #RRRRGGGGBBBB.

-background {#fde68a}
-background {#bfdbfe}-orient horizontal.% scrollbar .background -background {#336699}
> .background cget -background
#336699% scrollbar .background2 -background nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-borderwidth DISTANCE
The width of the border of the scrollbar.
The width of the border around the scrollbar. -bd is the same option. A distance is a count of pixels, or a number with c, i, m, or p for centimeters, inches, millimeters, or points.

-borderwidth 0
-borderwidth 1
-borderwidth 4-orient horizontal -relief solid.% scrollbar .borderwidth -borderwidth 4
> .borderwidth cget -borderwidth
4% scrollbar .borderwidth2 -borderwidth x
bad screen distance "x"
-errorcode TK VALUE PIXELS-command PREFIX
The command prefix that the scrollbar calls to scroll its widget.
When the user drags the slider or clicks the trough, the scrollbar adds the words of the change and runs the command at the global level. The words are moveto FRACTION, or scroll N units or scroll N pages. These are the arguments of xview and yview of a scrollable widget, so the usual value is {.list yview} for a listbox .list.
% scrollbar .command -command {set ::done 1}
> .command cget -command
set ::done 1ztk stores each prefix with no check.
-cursor CURSOR
The shape of the pointer over the scrollbar.
Each value side by side: the contact sheet of the cursors
A cursor name of Tk, such as hand2 or xterm. A value that starts with @ names a file. An empty value uses the cursor of the parent.
% scrollbar .cursor -cursor hand2
> .cursor cget -cursor
hand2% scrollbar .cursor2 -cursor nocursor
bad cursor spec "nocursor"
-errorcode TK VALUE CURSOR-elementborderwidth DISTANCE
The width of the borders of the slider and the arrows.
In Tk, the slider and the arrows have a 3-D border of this width, and -1 uses -borderwidth. ztk stores the value with no effect.
% scrollbar .elementborderwidth -elementborderwidth 4
> .elementborderwidth cget -elementborderwidth
4% scrollbar .elementborderwidth2 -elementborderwidth x
bad screen distance "x"
-errorcode TK VALUE PIXELS-highlightbackground COLOR
The color of the focus ring while the scrollbar has no focus, in the look of Tk on X11.
The color of the highlight ring of -highlightthickness while the scrollbar does not have the focus. ztk stores the value with no effect.
% scrollbar .highlightbackground -highlightbackground {#336699}
> .highlightbackground cget -highlightbackground
#336699% scrollbar .highlightbackground2 -highlightbackground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-highlightcolor COLOR
The color of the focus ring while the scrollbar has the focus, in the look of Tk on X11.
The color of the highlight ring of -highlightthickness while the scrollbar has the focus. ztk stores the value with no effect.
% scrollbar .highlightcolor -highlightcolor {#336699}
> .highlightcolor cget -highlightcolor
#336699% scrollbar .highlightcolor2 -highlightcolor nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-highlightthickness DISTANCE
The width of the focus ring, in the look of Tk on X11.
The width of the ring around the scrollbar that shows the focus. The modern theme draws the focus with the stylesheet.
% scrollbar .highlightthickness -highlightthickness 4
> .highlightthickness cget -highlightthickness
4% scrollbar .highlightthickness2 -highlightthickness x
bad screen distance "x"
-errorcode TK VALUE PIXELS-jump BOOLEAN
Whether a drag of the slider scrolls only at the release of the button.
With a false value, the scrollbar runs -command at each move of a drag. With a true value, it runs -command once, at the release.
% scrollbar .jump -jump 1
> .jump cget -jump
1% scrollbar .jump2 -jump maybe
expected boolean value but got "maybe"
-errorcode TCL VALUE NUMBER-orient WORD
The direction of the scrollbar: horizontal or vertical.
The scrollbar lies along this direction.
Values: vertical, horizontal.

-orient vertical
-orient horizontal% scrollbar .orient -orient horizontal
> .orient cget -orient
horizontal% scrollbar .orient2 -orient bogus
bad orientation "bogus": must be vertical or horizontal
-errorcode TCL LOOKUP INDEX orientation bogus-relief RELIEF
The 3-D look of the border.
Each value side by side: the contact sheet of the reliefs
The style of the border: raised and sunken look 3-D, and groove and ridge are lines. The theme draws the border, and the CSS of the theme can give a relief its own look with border-style.
Values: flat, groove, raised, ridge, solid, sunken.

-relief flat
-relief raised
-relief sunken
-relief solid-orient horizontal -borderwidth 2.% scrollbar .relief -relief groove
> .relief cget -relief
groove% scrollbar .relief2 -relief bumpy
bad relief "bumpy": must be flat, groove, raised, ridge, solid, or sunken
-errorcode TCL LOOKUP INDEX relief bumpy-repeatdelay N
The time in milliseconds before a held button starts to repeat.
In Tk, a press that is held on an arrow or the trough repeats its action after this delay. ztk stores the value with no effect: a press moves one step.
% scrollbar .repeatdelay -repeatdelay 5
> .repeatdelay cget -repeatdelay
5% scrollbar .repeatdelay2 -repeatdelay x
expected integer but got "x"
-errorcode TCL VALUE NUMBER-repeatinterval N
The time in milliseconds between two repeats of a held button.
In Tk, a held press repeats its action at this interval. ztk stores the value with no effect.
% scrollbar .repeatinterval -repeatinterval 5
> .repeatinterval cget -repeatinterval
5% scrollbar .repeatinterval2 -repeatinterval x
expected integer but got "x"
-errorcode TCL VALUE NUMBER-takefocus TEXT
Whether the Tab key can move the focus to the scrollbar.
1 puts the scrollbar in the order of the Tab key, and 0 leaves it out. An empty value uses the default rule of Tk for the widget. In Tk the value can also be a script, and its result is the answer. ztk stores each value with no check.
% scrollbar .takefocus -takefocus 1
> .takefocus cget -takefocus
1ztk stores each value of the option with no check.
-troughcolor COLOR
The color of the trough, in the look of Tk on X11.
The color of the long area that the slider moves in. ztk stores the value with no effect.
% scrollbar .troughcolor -troughcolor {#336699}
> .troughcolor cget -troughcolor
#336699% scrollbar .troughcolor2 -troughcolor nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-width DISTANCE
The width of the short side of the scrollbar.
The size of the scrollbar across its -orient direction. A distance is a count of pixels, or a number with c, i, m, or p for centimeters, inches, millimeters, or points.

-width 8
-width 15
-width 30-orient horizontal.% scrollbar .width -width 4
> .width cget -width
4% scrollbar .width2 -width x
bad screen distance "x"
-errorcode TK VALUE PIXELSBindings
| Event | Description |
|---|---|
<Button-1> | A press on the thumb starts a drag. A press on an arrow scrolls one unit, and a press on the trough scrolls one page, toward the press. |
<B1-Motion> | During a drag, the thumb follows the pointer, and the scrollbar runs -command with moveto. |
<ButtonRelease-1> | Ends the drag. |
<Button-2> | Moves the center of the thumb to the pointer. |
<MouseWheel> | Scrolls a vertical scrollbar, 4 units for each step of the wheel. With Shift, the wheel scrolls a horizontal scrollbar. |
<Key-Up> | Scrolls a vertical scrollbar one unit up. Down scrolls one unit down, and Left and Right do the same for a horizontal scrollbar. |
<Control-Key-Up> | Scrolls a vertical scrollbar one page up. Control-Down, Control-Left, and Control-Right scroll one page. |
<Key-Prior> | Scrolls one page back. Next scrolls one page forward. |
<Key-Home> | Runs -command with moveto 0. |
<Key-End> | Runs -command with moveto 1. |
Returns
PATHNAME.
Examples
# The listbox shows its first items, so the slider starts at 0.
% listbox .files -yscrollcommand {.vbar set}
> scrollbar .vbar -command {.files yview}
> pack .vbar -side right -fill y
> pack .files -side left -fill both -expand 1
> .files insert end {*}[lrepeat 40 item]
> update
> lindex [.vbar get] 0
0.0
% winfo class [scrollbar .go]
Scrollbar
Errors
% scrollbar .x -foo 1
unknown option "-foo"
-errorcode TK LOOKUP OPTION -foo
% scrollbar .twice
> scrollbar .twice
window name "twice" already exists in parent
-errorcode TK VALUE WINDOW EXISTS
# The parent .no does not exist.
% scrollbar .no.b
bad window path name ".no"
-errorcode TK LOOKUP WINDOW .no
% scrollbar
wrong # args: should be "scrollbar pathName ?-option value ...?"
-errorcode TCL WRONGARGS
Differences from Tcl
- The modern scrollbar has no arrows, so
identifynever givesarrow1orarrow2, andfractionanddeltacount from the ends of the trough. - A new scrollbar gives
0.0 1.0forget. Tk 8.6.16 gives0 0 0 0, the form of four numbers. - A press on the trough moves one step. Holding the button does not repeat, so
-repeatdelayand-repeatintervalhave no effect. - An error of a widget command has the
-errorcodeNONE, and the error of a bad value of-orient,-relief,-activerelief,-jump, or a distance has another code than in Tk 8.6.16, such asTCL LOOKUP INDEX orientation bogusforTK VALUE ORIENTATION. -activerelief,-elementborderwidth,-troughcolor,-highlightbackground, and-highlightcolorare stored with no effect.
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztk 1.0.0-beta.1