Ruby/TK - Notebook Widget

Advertisements


The NoteBook widget provides a notebook metaphor to display several windows in limited space. The notebook is divided into a stack of pages of which only one is displayed at any time.

The other pages can be selected by means of choosing the visual tabs at the top of the widget. Additionally, the <Tab> key may be used to traverse the pages. If underline option is used, Alt-bindings will also work.

Syntax:

Here is a simple syntax to create this widget:

Tk::Tile::Notebook.new(root) {
  .....Standard Options....
  .....Widget Specific Options....
}

Standard Options:

  • class

  • cursor

  • state

  • style

  • takefocus

Widget-specific Options:

SNOptions with Description
1height => Integer
If present and greater than zero, specifies the desired height of the pane area (not including internal padding or tabs). Otherwise, the maximum height of all panes is used.
2padding => Integer
Specifies the amount of extra space to add around the outside of the notebook. The padding is a list of up to four length specifications left top right bottom. If fewer than four elements are specified, bottom defaults to top, right defaults to left, and top defaults to left.
3width => Integer
If present and greater than zero, specifies the desired width of the pane area (not including internal padding). Otherwise, the maximum width of all panes is used.

Manipulating Notebook:

There are various ways to play with Notebook:

  • Each page on a Notebook is typically a frame, a direct child (subwindow) of the notebook itself. A new page and its associated tab are added to the end of the list of tabs with the "add subwindow ?option value...?" method.

  • The text tab option is used to set the label on the tab; also useful is the state tab option, which can have the value normal, disabled (not selectable), or hidden.

  • To insert a tab at somewhere other than the end of the list, you can use the "insert position subwindow ?option value...?", and to remove a given tab, use the forget method, passing it either the position (0..n-1) or the tab's subwindow. You can retrieve the list of all subwindows contained in the notebook via the tabs method.

  • To retrieve the subwindow that is currently selected, call the selected method, and change the selected tab by calling the select method, passing it either the tab's position or the subwindow itself as a parameter.

  • To change a tab option you can use the "itemconfigure tabid, :option => value" method. Where tabid is the tab's position or subwindow. You can use the "itemcget tabid, :option" to return the current value of the option.

Examples:

require 'tk'
require 'tkextlib/tile'

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

n = Tk::Tile::Notebook.new(root)do
  height 110
  place('height' => 100, 'width' => 200, 'x' => 10, 'y' => 10)
end

f1 = TkFrame.new(n)
f2 = TkFrame.new(n)
f3 = TkFrame.new(n)

n.add f1, :text => 'One', :state =>'disabled'
n.add f2, :text => 'Two'
n.add f3, :text => 'Three'

Tk.mainloop

This will produce the following result:

Ruby/Tk notebook


Advertisements
Advertisements