Copyright © tutorialspoint.com

Ruby/Tk - Entry Widget

previous next


Description:

A Entry presents the user with a single line text field that they can use to type in a value. These can be just about anything: their name, a city, a password, social security number, and so on.

Syntax:

Here is a simple syntax to create this widget:

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

Standard Options:

These options have been described in previous chapter.

Widget-specific Options:

SNOptions with Description
1disabledbackground => String
Specifies the background color to use when the entry is disabled. If this option is the empty string, the normal background color is used.
2disabledforeground => String
Specifies the foreground color to use when the entry is disabled. If this option is the empty string, the normal foreground color is used.
3readonlybackground => String
Specifies the background color to use when the entry is read-only. If this option is the empty string, the normal background color is used.
4show => String
If this option is specified, then the true contents of the entry are not displayed in the window. Instead, each character in the entry's value will be displayed as the first character in the value of this option, such as ``*''. This is useful, for example, if the entry is to be used to enter a password. If characters in the entry are selected and copied elsewhere, the information copied will be what is displayed, not the true contents of the entry.
5state => String
Specifies one of three states for the entry: normal, disabled, or readonly. If the entry is readonly, then the value may not be changed using widget commands and no insertion cursor will be displayed, even if the input focus is in the widget; the contents of the widget may still be selected. If the entry is disabled, the value may not be changed, no insertion cursor will be displayed, the contents will not be selectable, and the entry may be displayed in a different color.
6validate => 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.
7validatecommand => String
Specifies a script to eval when you want to validate the input into the entry widget.
8width => Integer
Specifies an integer value indicating the desired width of the entry window, in average-size characters of the widget's font. If the value is less than or equal to zero, the widget picks a size just large enough to hold its current text.

Validation of Entry:

We can validate entered value by setting the validatecommand option to a callback which will be evaluated according to the validate option as follows:

Manipulating Entries:

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

Event Bindings:

Ruby/Tk automatically creates class bindings for entries that give them the following default behavior. :

If the entry is disabled using the state option, then the entry's view can still be adjusted and text in the entry can still be selected, but no insertion cursor will be displayed and no text modifications will take place.

Examples:

require 'tk'

root = TkRoot.new
root.title = "Window"

entry1 = TkEntry.new(root)
entry2 = TkEntry.new(root) do
     show '*'
end

variable1 = TkVariable.new
variable2 = TkVariable.new
entry1.textvariable = variable1
entry2.textvariable = variable2
variable1.value = "Enter any text value"
variable2.value = "Enter any confidential value"

entry1.place('height' => 25,
            'width'  => 150,
            'x'      => 10,
            'y'      => 10)

entry2.place('height' => 25,
            'width'  => 150,
            'x'      => 10,
            'y'      => 40)

Tk.mainloop

This will produce following result

Ruby/Tk Entry

previous next

Copyright © tutorialspoint.com