ttk::scrollbar
Guide: Themed widgets (ttk)
Makes a themed scrollbar: a bar that shows and moves the visible part of another widget.
ttk::scrollbar PATHNAME ?OPTION VALUE ...?
Description
Makes a themed scrollbar with the name PATHNAME and the options of the call, and makes the command PATHNAME for the widget commands below. The scrollbar has a trough and a thumb. The size and the place of the thumb show the visible part of a widget that scrolls, such as a listbox, a text, or a ttk::treeview.
The link has two sides. The -yscrollcommand or -xscrollcommand of the other widget calls set of the scrollbar. A drag of the thumb, or a click on the trough, calls the -command of the scrollbar with moveto or scroll, and the command is the yview or xview of the other widget.
The parent of PATHNAME must exist, and PATHNAME must not. The class of the scrollbar is TScrollbar, the name of its style.
Widget commands
| Command | Description |
|---|---|
ttk::scrollbar set | Sets the visible part that the thumb shows. |
ttk::scrollbar get | Gives the two fractions of the last set. |
ttk::scrollbar delta | Gives the change of the fraction for a move of the thumb by some pixels. |
ttk::scrollbar fraction | Gives the fraction of the trough at a pixel point. |
ttk::scrollbar state | Gives the state flags of the widget, or turns flags on and off. |
ttk::scrollbar instate | Tests the state flags of the widget, and runs a script when they match. |
ttk::scrollbar identify | Gives the name of the element of the widget at a point. |
ttk::scrollbar cget | Gives the value of an option of the widget. |
ttk::scrollbar configure | Gives or sets the options of the widget. |
Options
| Option | Type | Default | Database name | Description |
|---|---|---|---|---|
-class | class name | empty | The class of the widget for the option database and for the bindings, set only at creation. | |
-command | script | empty | command | The command prefix that the scrollbar calls to scroll the other widget. |
-cursor | cursor name | empty | cursor | The shape of the pointer over the widget. |
-orient | orientation | vertical | orient | The direction of the scrollbar: vertical or horizontal. |
-style | style name | empty | style | The style of the widget, such as TScrollbar or a style of the script such as Accent.TScrollbar. |
-takefocus | boolean, empty, or script | empty | takeFocus | Whether the Tab key can move the focus to the widget. |
-class CLASS
The class of the widget for the option database and for the bindings, set only at creation.
The class name replaces the class of the style, such as TScrollbar, in winfo class and in the bind tags of the widget. Give it in the call that makes the widget. A later configure of the option gives an error.
% ttk::scrollbar .tsrcl -class Fancy
> winfo class .tsrcl
Fancy
% ttk::scrollbar .tsrcl3
> winfo class .tsrcl3
TScrollbar% ttk::scrollbar .tsrcl2
> .tsrcl2 configure -class Other
attempt to change read-only option
-errorcode NONE-command PREFIX
The command prefix that the scrollbar calls to scroll the other widget.
A drag of the thumb calls the prefix with moveto FRACTION appended. A click on the trough calls it with scroll COUNT pages appended, and the arrows of the theme classic with scroll COUNT units. The prefix is often {WIDGET yview} or {WIDGET xview}.
% ttk::scrollbar .tsrcm -command {.list yview}
> .tsrcm cget -command
.list yviewEach string is a valid prefix.
-cursor CURSOR
The shape of the pointer over the widget.
Each value side by side: the contact sheet of the cursors
A cursor name of Tk, such as hand2 or xterm. An empty value uses the cursor of the parent.
% ttk::scrollbar .tsrcu -cursor hand2
> .tsrcu cget -cursor
hand2ztk stores each value of the option with no check.
-orient ORIENT
The direction of the scrollbar: vertical or horizontal.
A vertical scrollbar scrolls up and down, and a horizontal scrollbar scrolls left and right.
Values: horizontal, vertical.

-orient vertical
-orient horizontal% ttk::scrollbar .tsror -orient horizontal
> .tsror cget -orient
horizontal% ttk::scrollbar .tsror2 -orient diagonal
bad orient "diagonal": must be horizontal or vertical
-errorcode TCL LOOKUP INDEX orient diagonal-style STYLE
The style of the widget, such as TScrollbar or a style of the script such as Accent.TScrollbar.
An empty value uses the style TScrollbar. A style name of the script ends with the name of the base style, such as Accent.TScrollbar, and ttk::style configure and ttk::style map give it its options. A style with no settings looks like its base style.
% ttk::style configure Accent.TScrollbar -foreground white
> ttk::scrollbar .tsrsy -style Accent.TScrollbar
> .tsrsy cget -style
Accent.TScrollbarztk stores each value of the option with no check, as Tk 8.6.16 does at creation.
-takefocus VALUE
Whether the Tab key can move the focus to the widget.
1 puts the widget in the order of the Tab key, and 0 leaves it out. The default empty value uses the default rule of Tk, and this widget is not in the order of the Tab key with it. A script runs with the path of the widget appended, and its result is the answer.
% ttk::scrollbar .tsrtf -takefocus 1
> .tsrtf cget -takefocus
1ztk stores each value of the option with no check.
Bindings
| 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
% listbox .items -yscrollcommand {.sb set}
> ttk::scrollbar .sb -command {.items yview}
> for {set i 0} {$i < 100} {incr i} {.items insert end "Item $i"}
> pack .items .sb -side left -fill y
> update
> lindex [.sb get] 0
0.0
% winfo class [ttk::scrollbar .vs]
TScrollbar
Errors
# The style sets the width of a themed scrollbar.
% ttk::scrollbar .x -width 10
unknown option "-width"
-errorcode TK LOOKUP OPTION -width
% ttk::scrollbar .twice
> ttk::scrollbar .twice
window name "twice" already exists in parent
-errorcode TK VALUE WINDOW EXISTS
# The parent .no does not exist.
% ttk::scrollbar .no.s
bad window path name ".no"
-errorcode TK LOOKUP WINDOW .no
% ttk::scrollbar
wrong # args: should be "ttk::scrollbar pathName ?-option value ...?"
-errorcode TCL WRONGARGS
Differences from Tcl
-cursoraccepts each value with no check. Tk 8.6.16 gives an error for a cursor name that does not exist.- The errors of
state,instate,identify, and of an unknown widget command have the error codeNONE. Tk 8.6.16 gives a code such asTTK VALUE STATEorTCL LOOKUP INDEX. The error of-underlinehas the codeTCL VALUE NUMBER, where Tk 8.6.16 givesTCL VALUE INTEGER. - The states
activeandhoverare one flag, sostate activeturns on both. - The errors of
set,get,delta, andfractionhave the error codeNONE, and the error of a bad pixel ofdeltaisexpected integer. Tk 8.6.16 givesTCL WRONGARGSorTCL VALUE NUMBER, andexpected floating-point number. - The modern theme draws no arrows, so
identifygivestroughorthumb, and the pixels ofdeltaandfractiondiffer from Tk 8.6.16. The themeclassichas the look of Tk on X11.
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztk 1.0.0-beta.1