15.14 Tkでメニューを表示する

use Tk;

$main = MainWindow->new();

# メニューを表示するための水平方向の領域をウィンドウの最上部に作成する
$menubar = $main
  ->Frame(-relief => "raised", -borderwidth => 2)
  ->pack(-anchor => "nw", -fill => "x");

# メニューを開くための"File"と言うラベルのボタンを作成する
$file_menu = $menubar
  ->Menubutton(-text => "File", -underline => 1)
  ->pack(-side => "left");

# Fileメニューのエントリを作成する
$file_menu->command(-label => "Print", -command => \&Print);
  • -menuitemsショートカットを使用する
$file_menu = $menubar
  ->Menubtutton(
    -text => "File",
    -underline => 1,
    -menuitems => [
      [ Button => "Print", -command => \&Print ],
      [ Button => "Save", -command => \&Save  ] ])
  ->pack(-side => "left");