Version 4 of scrollbar

Updated 2002-06-03 17:58:08

http://purl.org/tcl/home/man/tcl8.4/TkCmd/scrollbar.htm


"you want to pack your vertical Scrollbars -fill y and your horizontal Scrollbars -fill x ..."


See also Scroll bars that appear only when needed


GPS customizes scrollbars. He shares at least three somewhere on his site ...


On news:comp.lang.tcl , Ken Jones writes:

In your example, you created the Text and Scrollbar widgets, but you didn't wire them up to each other. Scrollbars have a -command which they call whenever the user interacts with them. Scrollable widgets also have -xscrollcommand and -yscrollcommand options so that they can communicate with their attached Scrollbars (for example, so the Scrollbar knows how large to make the "elevator" and where to place it in the trough).

An example of hooking up a vertical Scrollbar:

 text .t -yscrollcommand {.sbar set}
 scrollbar .sbar -orient vertical -command {.t yview}

An example of hooking up a horizontal Scrollbar

 text .t -xscrollcommand {.sbar set}
 scrollbar .sbar -orient horizontal -command {.t xview}

All scrollable widgets (Canvas, Entry, Listbox, Text) in Tk use the same mechanism. (Although scrolling Canvas widgets is trickier. If you need to scroll a Canvas, check out the Canvas's -scrollregion option as well.)

Also, it looks as though you need to study the pack command more. It's a bit difficult to describe concisely how it works. Here's my best shot at the fundamentals of packing:

  • Tcl lays out widgets in the order you pack them.
  • Each widget is allocated "packing space" (sometimes called a "parcel") in the space that's left over from packing previous widgets, which is called the "cavity."
  • A widget is allocated the entire side of the cavity, as specified by its pack -side option (default is top).
  • The parcel is just wide enough to accommodate the widget when

packing -side left or -side right, or just tall enough to accommodate it when packing -side top or -side bottom.

  • If a widget is smaller than its allocated packing space, pack centers the widget inside of its packing space by default.
  • After packing all the widgets, Tcl "shrink-wraps" the top-level window as tightly as it can around the widgets, squeezing out all the excess space it can.

You can then modify the behavior of the packer through various options like -fill and -anchor, but I'll let you investigate those on your own. But when it comes to Scrollbars, you want to pack your vertical Scrollbars -fill y and your horizontal Scrollbars -fill x, other wise they'll appear in their default size, which is almost never the behavior you want.

So, putting it all together in your example (vertical scrolling):

 #!/usr/bin/wish
 text .xstdin -yscrollcommand {.s set}

 # I'm not sure what you meant to do with this next line.
 # For the sake of illustration, I'm assuming that you
 # just wanted to display the output of some cat
 # command in your Text widget. There are better
 # ways of doing this, but that's another topic.

 .xstdin insert 1.0 [exec cat somefile]

 scrollbar .s -orient vertical -command {.xstdin yview}
 button .b1 -text Exit -command exit

 # .b1 gets the entire bottom of the window.
 pack .b1 -side bottom

 # Pack .s next, so that if the user decreases the size
  # of the window, the Text widget shrinks instead of
  # the Scrollbar. .s gets the right-hand side of the space
 # left over. It fills it vertically.
 pack .s -side right -fill y

 # Pack .xstdin last. Expand its packing space into
 # any additional space that becomes available if the
 # user resizes the window. Have the Text widget
 # completely fill its packing space.
 pack .xstdin -expand yes -fill both
 bell

 # Sleep 5 minutes (300,000 milliseconds) then close window
 after 900000 exit

 # No need to use tkwait to explicitly enter the
 # event loop. wish enters the event loop automatically
 # at the bottom of the script.


Tk syntax help - Arts and crafts of Tcl-Tk programming - Category Command