Tkinter入门
前页 | Chapter 32. The Menu Widget | 后页

# Methods

The `Menu` widget supports the standard Tkinter Widget interface (with the exception of the geometry manager methods), plus the following methods:

`add(type, options...)`

  Add (append) an entry of the given type to the menu. The type argument can be one of "command", "cascade" (submenu), "checkbutton", "radiobutton", or "separator". The options are as defined in the following table:

  Table 32-1. Menu Item Options

  Option | Type | Description
  --- | --- | ---
  `activebackground` | color |
  `activeforeground` | color |
  `accelerator` | string |
  `background` | color |
  `bitmap` | bitmap |
  `columnbreak` | flag |
  `command` | callback |
  `font` | font |
  `foreground` | color |
  `hidemargin` | flag |
  `image` | image |
  `indicatoron` | flag |
  `label` | string |
  `menu` | widget |
  `offvalue` | value |
  `onvalue` | value |
  `selectcolor` | color |
  `selectimage` | image |
  `state` | constant |
  `underline` | integer |
  `value` | value |
  `variable` | variable |

`add_cascade(options...)`, `add_checkbutton(options...)`, `add_command(options...)`, `add_radiobutton(options...)`, `add_separator(options...)`

  Convenience functions, used to add items of the given type.

`insert(index, type, options...)`, `insert_cascade(index, options...)`, `insert_checkbutton(index, options...)`, `insert_command(index, options...)`, `insert_radiobutton(index, options...)`, `insert_separator(index, options...)`

  Same as `add` and friends, but inserts the new item at the given index.

`entryconfig(index, options...)`, `entryconfigure(index, options...)`

  Reconfigure the given menu entry. Only the given options are changed; the rest are left as is.

`index(index) => integer`

  Convert an index (of any kind) to an integer index.

`delete(index)`, `delete(start, stop)`

  Delete one or more menu entries.

## Displaying Menus

`invoke(index)`

  Invoke the given entry (just like if the user had clicked on it).

`post(x, y)`

  Display the menu at the given position. The position should be given in pixels, relative to the root window.

`unpost()`

  Remove a posted menu.

`yposition(index) => integer`

  Return the vertical offset for the given entry. This can be used to position a popup menu so that a given entry is under the the mouse when the menu appears.

前页 | Home | 后页
Patterns | Up | Options