Ruby/TK - Label Widget



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.


Here is a simple syntax to create this widget − {
   .....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

height => Integer

Specifies a desired height for the label.


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.


require 'tk'

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

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


This will produce the following result −

Ruby/Tk Label