Copyright © tutorialspoint.com

Ruby/Tk - Spinbox Widget

previous next


Description:

A Spinbox widget allows users to choose numbers (or in fact, items from an arbitrary list). It does this by combining an entry-like widget showing the current value with a pair of small up/down arrows which can be used to step through the range of possible choices.

Spinboxes are capable of displaying strings that are too long to fit entirely within the widget's window. In this case, only a portion of the string will be displayed; commands described below may be used to change the view in the window.

Spinboxes use the standard xscrollcommand mechanism for interacting with scrollbars.

Syntax:

Here is a simple syntax to create this widget:

TkSpinbox.new(root) {
  .....Standard Options....
  .....Widget-specific Options....
}

Standard Options:

These options have been described in previous chapter.

Widget-specific Options:

SNOptions with Description
1buttonbackground => String
The background color to be used for the spin buttons.
2buttoncursor => String
The cursor to be used when over the spin buttons. If this is empty (the default), a default cursor will be used.
3buttondownrelief => String
The relief to be used for the upper spin button.
4command => String
Specifies a Ruby/Tk callback to invoke whenever a Spinbutton is invoked. The callback has these two arguments appended to any existing callback arguments: the current value of the widget and the direction of the button press (up or down).
5disabledbackground => String
Specifies the background color to use when the Spinbox is disabled. If this option is the empty string, the normal background color is used.
6disabledforeground => String
Specifies the foreground color to use when the Spinbox is disabled. If this option is the empty string, the normal foreground color is used.
7format => String
Specifies an alternate format to use when setting the string value when using the from and to range.
8from => Integer
A floating-point value corresponding to the lowest value for a Spinbox, to be used in conjunction with to and increment.
9increment => String
A floating-point value specifying the increment. When used with from and to, the value in the widget will be adjusted by increment when a spin button is pressed (up adds the value, down subtracts the value).
10state => String
Specifies one of three states for the Spinbox: normal, disabled, or readonly.
11to => Integer
A floating-point value corresponding to the highest value for the Spinbox, to be used in conjunction with from and increment. When all are specified correctly, the Spinbox will use these values to control its contents. This value must be greater than the from option. If values is specified, it supercedes this option.
12validate => String
Specifies the mode in which validation should operate: none, focus, focusin, focusout, key, or all. It defaults to none. When you want validation, you must explicitly state which mode you wish to use.
13validatecommand => String
Specifies a script to evaluate when you want to validate the input in the widget.
14values => Integer
Must be a proper list value. If specified, the Spinbox will use these values as to control its contents, starting with the first value. This option has precedence over the from and to range.
15width => Integer
Specifies an integer value indicating the desired width of the Spinbox window, in average-size characters of the widget's font.
16wrap => Boolean
Must be a proper boolean value. If on, the Spinbox will wrap around the values of data in the widget.

Validation Stages:

Validation works by setting the validatecommand option to a callback which will be evaluated according to the validate option as follows:

Manipulating Spinbox:

Here is a list of few important methods to play with Spinbox:

Event Bindings:

Tk automatically creates class bindings for Spinboxes that give them default behavior. Few important behaviors are given below:

Examples:

require 'tk'

root = TkRoot.new
root.title = "Window"
Sb = TkSpinbox.new(root) do
  to 100
  from 5
  increment 5
  pack("side" => "left",  "padx"=> "50", "pady"=> "50")
end

Tk.mainloop

This will produce following result

Ruby/Tk Spin Box

previous next

Copyright © tutorialspoint.com