Unless you plan on drawing very small pictures, the previous example
may not be all that useful.
TkCanvas
,
TkListbox
, and
TkText
can be set up to use scrollbars, so you can work on a
smaller subset of the ``big picture.''
Communication between a scrollbar and a widget is
bidirectional. Moving the scrollbar means that the widget's view
has to change; but when the widget's view is changed by some other
means, the scrollbar has to change as well to reflect the new
position.
Since we haven't done much with lists yet, our scrolling example will
use a scrolling list of text. In the following code fragment, we'll
start off by creating a plain old
TkListbox
. Then, we'll make a
TkScrollbar
. The scrollbar's callback (set with
command
)
will call the list widget's
yview
method, which will change the
value of the visible portion of the list in the y-direction.
After that callback is set up, we make the inverse association: when the list
feels the need to scroll, we'll set the appropriate range in the
scrollbar using
TkScrollbar#set
.
We'll use this same fragment in a fully functional program in the next
section.
list_w = TkListbox.new(frame, 'selectmode' => 'single')
scroll_bar = TkScrollbar.new(frame,
'command' => proc { |*args| list_w.yview *args })
scroll_bar.pack('side' => 'left', 'fill' => 'y')
list_w.yscrollcommand(proc { |first,last|
scroll_bar.set(first,last) })
|