Copyright © tutorialspoint.com

Ruby/Tk - Scale Widget

previous next


Description:

A Scale is a widget that displays a rectangular trough and a small slider. The trough corresponds to a range of real values (determined by the from, to, and resolution options), and the position of the slider selects a particular real value.

Three annotations may be displayed in a scale widget:

Each of these three annotations may be enabled or disabled using the configuration options.

Syntax:

Here is a simple syntax to create this widget:

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

Standard Options:

These options have been described in previous chapter.

Widget-specific Options:

SNOptions with Description
1bigincrement =>Integer
Some interactions with the scale cause its value to change by large increments; this option specifies the size of the large increments. If specified as 0, the large increments default to 1/10 the range of the scale.
2command =>String
Specifies the prefix of a Ruby/Tk callback to invoke whenever the scale's value is changed via a method.
3digits =>Integer
An integer specifying how many significant digits should be retained when converting the value of the scale to a string. If the number is less than or equal to zero, then the scale picks the smallest value that guarantees that every possible slider position prints as a different string.
4from =>Integer
A real value corresponding to the left or top end of the scale.
5label =>String
A string to display as a label for the scale. For vertical scales the label is displayed just to the right of the top end of the scale. For horizontal scales the label is displayed just above the left end of the scale.
6length =>Integer
Specifies the desired long dimension of the scale in screen units
7resolution =>Integer
A real value specifying the resolution for the scale. If this value is greater than zero then the scale's value will always be rounded to an even multiple of this value, as will tick marks and the endpoints of the scale. If the value is less than zero then no rounding occurs. Defaults to 1
8showvalue =>Boolean
Specifies a boolean value indicating whether or not the current value of the scale is to be displayed.
9sliderlength =>Integer
Specfies the size of the slider, measured in screen units along the slider's long dimension.
10sliderrelief =>String
Specifies the relief to use when drawing the slider, such as raised or sunken.
11state =>String
Specifies one of three states for the scale: normal, active, or disabled.
12tickinterval =>Integer
Must be a real value. Determines the spacing between numerical tick marks displayed below or to the left of the slider. If 0, no tick marks will be displayed.
13to =>Integer
Specifies a real value corresponding to the right or bottom end of the scale. This value may be either less than or greater than the from option.
14variable =>Variable
Specifies the name of a global variable to link to the scale. Whenever the value of the variable changes, the scale will update to reflect this value. Whenever the scale is manipulated interactively, the variable will be modified to reflect the scale's new value.
15width =>Integer
Specifies the desired narrow dimension of the trough in screen units

Manipulating Scales:

The following methods are available for scale widgets:

Event Bindings:

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

If the scale is disabled using the state option then none of the above bindings have any effect.

Examples:

require "tk"

$scale = TkScale.new {
  orient 'horizontal'
  length 280
  from 0
  to 250
  command (proc {printheight})
  tickinterval 50
  pack
}

def printheight
  height = $scale.get()
  print height, "\n"
end

Tk.mainloop

This will produce following result

Ruby/Tk Scale

previous next

Copyright © tutorialspoint.com