Copyright © tutorialspoint.com

Ruby/Tk - Menu Widget

previous next


Description:

A menu is a widget that displays a collection of one-line entries arranged in one or more columns. There exist several different types of entries, each with different properties. Entries of different types may be combined in a single menu. Menu entries are not the same as entry widgets. In fact, menu entries are not even distinct widgets; the entire menu is one widget.

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:

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

Standard Options:

These options have been described in previous chapter.

Widget-specific Options:

SNOptions with Description
1postcommand => String
If this option is specified then it provides a callback to execute each time the menu is posted. The callback is invoked by the post method before posting the menu.
2selectcolor => String
For menu entries that are check buttons or radio buttons, this option specifies the color to display in the indicator when the check button or radio button is selected.
3tearoff => Integer
This option must have a proper boolean value, which specifies whether or not the menu should include a tear-off entry at the top. If so, it will exist as entry 0 of the menu and the other entries will number starting at 1. The default menu bindings arrange for the menu to be torn off when the tear-off entry is invoked.
4tearoffcommand => String
If this option has a non-empty value, then it specifies a Ruby/Tk callback to invoke whenever the menu is torn off. The actual command will consist of the value of this option, followed by a space, followed by the name of the menu window, followed by a space, followed by the name of the name of the torn off menu window. For example, if the option's is "a b" and menu .x.y is torn off to create a new menu .x.tearoff1, then the command "a b .x.y .x.tearoff1" will be invoked.
5title => String
The string will be used to title the window created when this menu is torn off. If the title is NULL, then the window will have the title of the menubutton or the text of the cascade item from which this menu was invoked.
6type => String
This option can be one of menubar,tearoff, or normal, and is set when the menu is created.

Manipulating the Menus:

There are various ways to play with a Menus:

Menu Configuration:

The default bindings support four different ways of using menus:

Event Bindings:

Ruby/Tk automatically creates class bindings for menus that give them the following default behavior:

Disabled menu entries are non-responsive: they don't activate and they ignore mouse button presses and releases.

Examples:

require "tk"

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


menu_click = Proc.new {
  Tk.messageBox(
    'type'    => "ok",  
    'icon'    => "info",
    'title'   => "Title",
    'message' => "Message"
  )
}

file_menu = TkMenu.new(root)

file_menu.add('command',
              'label'     => "New...",
              'command'   => menu_click,
              'underline' => 0)
file_menu.add('command',
              'label'     => "Open...",
              'command'   => menu_click,
              'underline' => 0)
file_menu.add('command',
              'label'     => "Close",
              'command'   => menu_click,
              'underline' => 0)
file_menu.add('separator')
file_menu.add('command',
              'label'     => "Save",
              'command'   => menu_click,
              'underline' => 0)
file_menu.add('command',
              'label'     => "Save As...",
              'command'   => menu_click,
              'underline' => 5)
file_menu.add('separator')
file_menu.add('command',
              'label'     => "Exit",
              'command'   => menu_click,
              'underline' => 3)

menu_bar = TkMenu.new
menu_bar.add('cascade',
             'menu'  => file_menu,
             'label' => "File")

root.menu(menu_bar)

Tk.mainloop

This will produce following result

Ruby/Tk Menu

previous next

Copyright © tutorialspoint.com