Copyright © tutorialspoint.com

Ruby/Tk - Scrollbar Widget

previous next


Description:

A Scrollbar helps the user to see all parts of another widget, whose content is typically much larger than what can be shown in the available screen space.

A scrollbar displays two arrows, one at each end of the scrollbar, and a slider in the middle portion of the scrollbar. The position and size of the slider indicate which portion of the document is visible in the associated window.

Syntax:

Here is a simple syntax to create this widget:

TkScrollbar.new{
  .....Standard Options....
  .....Widget-specific Options....
}

Standard Options:

These options have been described in previous chapter.

Widget-specific Options:

SNOptions with Description
1activerelief => String
Specifies the relief to use when displaying the element that is active, if any. Elements other than the active element are always displayed with a raised relief.
2command => String
Specifies a callback to invoke to change the view in the widget associated with the scrollbar. When a user requests a view change by manipulating the scrollbar, the callback is invoked.
3elementborderwidth => Integer
Specifies the width of borders drawn around the internal elements of the scrollbar.
4width => Integer
Specifies the desired narrow dimension of the scrollbar window, not including 3-D border, if any. For vertical scrollbars this will be the width and for horizontal scrollbars this will be the height.

Elements of Scrollbar:

A scrollbar displays five elements, which are referred to in the methods for the scrollbar:

Manipulating Scrollbar:

There are following useful methods to manipulate the content of an scrollbar:

Event Bindings:

Ruby/Tk automatically creates class bindings for scrollbars that give them the following default behavior. If the behavior is different for vertical and horizontal scrollbars, the horizontal behavior is described in parentheses:

Examples:

require "tk"

list = scroll = nil

list = TkListbox.new {
  yscroll proc{|idx|
	scroll.set *idx
  }
  width 20
  height 16
  setgrid 1
  pack('side' => 'left', 'fill' => 'y', 'expand' => 1)
}
scroll = TkScrollbar.new {
  command proc{|idx|
	list.yview *idx
  }
  pack('side' => 'left', 'fill' => 'y', 'expand' => 1)
}

for f in Dir.glob("*")
  list.insert 'end', f
end

Tk.mainloop

This will produce following result

Ruby/Tk Scrollbar

previous next

Copyright © tutorialspoint.com