Copyright © tutorialspoint.com

Ruby/Tk - ListBox Widget

previous next


Description:

A radiobutton displays a list of single-line text items, usually lengthy, and allows the user to browse through the list, selecting one or more.

When first created, a new listbox has no elements. Elements may be added or deleted using provided methods. In addition, one or more elements may be selected from the listed items.

It is not necessary for all the elements to be displayed in the listbox window at once. Listboxes allow scrolling in both directions using the standard xscrollcommand and yscrollcommand options

Syntax:

Here is a simple syntax to create this widget:

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

Standard Options:

These options have been described in previous chapter.

Widget-specific Options:

SNOptions with Description
1activestyle => String
Specifies the style in which to draw the active element. This must be one of dotbox, none or underline. The default is underline.
2height => Integer
Specifies the desired height for the window, in lines. If zero or less, then the desired height for the window is made just large enough to hold all the elements in the listbox.
3listvariable => Variable
Specifies the reference of a variable. The value of the variable is an array to be displayed inside the widget; if the variable value changes then the widget will automatically update itself to reflect the new value.
4selectmode => String
Specifies one of several styles for manipulating the selection. The value of the option may be arbitrary, but the default bindings expect it to be either single, browse, multiple, or extended; the default value is browse.
5state => String
Specifies one of two states for the listbox: normal or disabled. If the listbox is disabled then items may not be inserted or deleted.
6width => Integer
Specifies the desired width for the window in 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. If zero or less, then the desired width for the window is made just large enough to hold all the elements in the listbox.

Manipulating the Listbox Items:

There are various ways to play with a list box:

Indices:

Many of the methods for listboxes take one or more indices as arguments. An index specifies a particular element of the listbox, in any of the following ways:

Event Bindings:

Ruby/Tk creates class bindings for listboxes that give them Motif-like behavior. Much of the behavior of a listbox is determined by its selectmode option, which selects one of four ways of dealing with the selection.

Most people will probably want to use browse mode for single selections and extended mode for multiple selections; the other modes appear to be useful only in special situations.

In addition to the above behavior, there are many other additional behavior associated with a listbox which are not covered in this tutorial:

Example 1:

require "tk"

root = TkRoot.new
root.title = "Window"
list = TkListbox.new(root) do
  width 20
  height 10
  setgrid 1
  selectmode 'multiple'
  pack('fill' => 'x')
end

list.insert 0, "yellow", "gray", "green",
  "blue", "red", "black", "white", "cyan",
  "pink", "yellow", "orange", "gray"

Tk.mainloop

This will produce following result

Ruby/Tk List Box 1

Example 2:

Following is the example using listvariable option to populate list items:

require "tk"

$names = %w{ yellow gray green
              blue red black white cyan
              pink yellow orange gray}
$colornames = TkVariable.new($names)

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

list = TkListbox.new(root) do
  width 20
  height 10
  setgrid 1
  listvariable $colornames
  pack('fill' => 'x')
end

Tk.mainloop

This will produce following result

Ruby/Tk List Box 2

Example 3:

Following example explains how to use TkScrollbar widget along with list box.

require "tk"

$names = %w{ yellow gray green
              blue red black white cyan
              pink yellow orange gray}
$colornames = TkVariable.new($names)

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

list = TkListbox.new(root) do
  listvariable $colornames
  pack('fill' => 'x')
end

list.place('height' => 150,
           'width'  => 100,
           'x'      => 10,
           'y'      => 10)

scroll = TkScrollbar.new(root) do
    orient 'vertical'
    place('height' => 150, 'x' => 110)
end

list.yscrollcommand(proc { |*args|
  scroll.set(*args)
})

scroll.command(proc { |*args|
  list.yview(*args)
}) 

Tk.mainloop

This will produce following result

Ruby/Tk List Box 3

previous next

Copyright © tutorialspoint.com