Copyright © tutorialspoint.com

Ruby/Tk - Text Widget

previous next


Description:

A Text widget provides users with an area so that they can enter multiple lines of text. Text widgets are part of the classic Tk widgets, not the themed Tk widgets.

Text widgets support three different kinds of annotations on the text:

A label can display a textual string, bitmap or image. If text is displayed, it must all be in a single font, but it can occupy multiple lines on the screen (if it contains newlines or if wrapping occurs because of the wraplength option) and one of the characters may optionally be underlined using the underline option.

Syntax:

Here is a simple syntax to create this widget:

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

Standard Options:

These options have been described in previous chapter.

Widget-specific Options:

SNOptions with Description
1height => Integer
Specifies the desired height for the window, in units of characters. Must be at least one.
2spacing1 => Integer
Requests additional space above each text line in the widget, using any of the standard forms for screen distances. If a line wraps, this option only applies to the first line on the display. This option may be overriden with spacing1 options in tags.
3spacing2 => Integer
For lines that wrap (so that they cover more than one line on the display) this option specifies additional space to provide between the display lines that represent a single line of text. The value may have any of the standard forms for screen distances. This option may be overriden with spacing options in tags.
4spacing3 => Integer
Requests additional space below each text line in the widget, using any of the standard forms for screen distances. If a line wraps, this option only applies to the last line on the display. This option may be overriden with spacing3 options in tags.
5state => String
Specifies one of two states for the text: normal or disabled. If the text is disabled then characters may not be inserted or deleted and no insertion cursor will be displayed, even if the input focus is in the widget.
6tabs => String
Specifies a set of tab stops for the window. The option's value consists of a list of screen distances giving the positions of the tab stops. Each position may optionally be followed in the next list element by one of the keywords left, right, center, or numeric, which specifies how to justify text relative to the tab stop. Left is the default
7width => Integer
Specifies the desired width for the window in units of characters. If the font doesn't have a uniform width then the width of the character "0" is used in translating from character units to screen units.
8wrap => String
Specifies how to handle lines in the text that are too long to be displayed in a single line of the text's window. The value must be none or char or word.

Manipulating Test:

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

Event Bindings:

Ruby/Tk automatically creates class bindings for texts. Here are few important bindings listed.

Examples:

require 'tk'

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

text = TkText.new(root) do
  width 30
  height 20
  borderwidth 1
  font TkFont.new('times 12 bold')
   pack("side" => "right",  "padx"=> "5", "pady"=> "5")
end
text.insert 'end', "Hello!\n\ntext widget example"
Tk.mainloop

This will produce following result

Ruby/Tk Text

previous next

Copyright © tutorialspoint.com