Ruby/TK - Label Widget



Description

A label is a widget that displays text or images, typically that the user will just view but not otherwise interact with. Labels are used for such things as identifying controls or other parts of the user interface, providing textual feedback or results, etc.

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 −

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

Standard Options

  • anchor
  • background
  • bitmap
  • borderwidth
  • cursor
  • font
  • foreground
  • highlightbackground
  • highlightcolor
  • highlightthickness
  • image
  • justify
  • padx
  • pady
  • relief
  • takefocus
  • text
  • textvariable
  • underline
  • wraplength

These options have been described in the previous chapter.

Widget Specific Options

Sr.No. Options & Description
1

height => Integer

Specifies a desired height for the label.

2

width => Integer

Specifies a desired width for the label.

Event Bindings

When a new label is created, it has no default event bindings: labels are not intended to be interactive.

Examples

require 'tk'

$resultsVar = TkVariable.new
root = TkRoot.new
root.title = "Window"
Lbl = TkLabel.new(root) do
   textvariable
   borderwidth 5
   font TkFont.new('times 20 bold')
   foreground  "red"
   relief      "groove"
   pack("side" => "right",  "padx"=> "50", "pady"=> "50")
end

Lbl['textvariable'] = $resultsVar
$resultsVar.value = 'New value to display'

Tk.mainloop

This will produce the following result −

Ruby/Tk Label
ruby_tk_guide.htm
Advertisements