ボタンを押すことで,アクションがコールバックされるGUIの基本的ウィジェットです.
書式: $child = $parent->Button(?options?)
主なオプション:
オプション | 説明 | オプション値の例 |
-text => textstring | ボタンにつける文字列を指定する. | "ボタン1", "OK" |
-textvariable => \$variable | ボタンにつける文字列を変数で指定する. | \$but_label |
-command => @command | ボタンを押すことで,実行されるコールバックルーチンを指定する.無名配列,または,無名サブルーチンで指定する. | [ \&Print_msg, $msg ],sub{ Print_msg($msg);} |
-relief => style | ボタンのスタイルを指定する. | 'raised', 'groove', 'ridge', 'solid', 'sunken', 'flat' |
-foreground => color | ボタンの文字色 | 'red', '#ff0000' |
-background => color | ボタンの背景色 | 'red', '#ff0000' |
-height => h | ボタンのY方向の幅 | 12, 24 |
-width => w | ボタンのX方向の幅 | 12, 24 |
-bitmap => bitmap | X ビットマップ(XBM)形式の画像を貼る. | '@filename.xbm', "\@filename.xbm" |
-image => image | GIF等の画像ファイルを貼る.Photoメソッドと併用して使う. | -image=> $mw->Photo (-file=> 'picture.gif') |
-anchor => anchor | ボタン内のテキストアンカー | 'n', 'e', 'w', 's' ,'c'の組み合わせ |
-padx (pady) => amount | ボタンの端とテキストの間の隙間 | 12, 24 |
文字列や画像などGUI上のラベルをつけるためのウィジェットです.何のイベント・アクションも起こしませんが,見栄えが命であるGUIにおいて,必須のものです.オプションはボタンのものと大体同様です.
書式: $child = $parent->Label(?options?)
主なオプション:
オプション | 説明 | オプション値の例 |
-text => textstring | ラベルにつける文字列を指定する. | "ボタン1", "OK" |
-textvariable => \$variable | ラベルにつける文字列を変数で指定する. | \$but_label |
-relief => style | ラベルのスタイルを指定する. | 'raised', 'groove', 'ridge', 'solid', 'sunken', 'flat' |
-foreground => color | ラベルの文字色 | 'red', '#ff0000' |
-background => color | ラベルの背景色 | 'red', '#ff0000' |
-height => h | ラベルのY方向の幅 | 12, 24 |
-width => w | ラベルのX方向の幅 | 12, 24 |
-bitmap => bitmap | X ビットマップ(XBM)形式の画像を貼る. | '@filename.xbm', "\@filename.xbm" |
-image => image | GIF等の画像ファイルを貼る.Photoメソッドと併用して使う. | -image=> $mw->Photo (-file=> 'picture.gif') |
-anchor => anchor | ラベル内のテキストアンカー | 'n', 'e', 'w', 's' ,'c'の組み合わせ |
-padx (pady) => amount | ラベルの端とテキストの間の隙間 | 12, 24 |
2種類のウィジェットとpackを使って,簡単なGUIを作成してみましょう.以下にサンプルを示します.なお,プログラムと同じディレクトリに " Xcamel.gif "という画像ファイルを置いています.
#!/usr/bin/perl -w use Tk; %doremi = ( "ド", "ドはドーナツのド", "レ", "レはレモンのレ", "ミ", "ミはみんなのミ", "ファ", "ファはファイトのファ", "ソ", "ソは青い空", "ラ", "ラはラッパのラ", "シ", "シは幸せよ" ); @bgcolor = qw/red orange yellow green cyan blue purple/; $msg = "歌いましょう!! 右のボタンを押してください"; $mw = MainWindow->new; $mw->title('Do-re-mi'); $mw->Label(-text=>"Perl/Tkラクダのドレミ", -relief=> 'ridge', -padx => 5, -pady => 5, -anchor=> 'nw', -foreground => 'red') ->pack(-fill=>'both', -expand=>'yes', -side=>'top'); $mw->Button(-text => "終了", -command => sub { exit; } ) ->pack(-fill=>'both', -expand=> 'yes', -side=>'bottom'); $mw->Label(-image=> $mw->Photo(-file=>'Xcamel.gif'), -borderwidth=>3, -relief=> 'sunken') -> pack(-side=>'left', -padx=> 5, -pady=> 5); $mw->Label(-textvariable => \$msg, -anchor=> 'w', -background=> 'white', -relief=>'sunken', -width => 40 ) -> pack(-side=>'left', -padx=>5); foreach $i (qw/ド レ ミ ファ ソ ラ シ/){ $mw->Button(-text=> $i, -width=> 3, -background => shift(@bgcolor), -command => [ \&Sing_msg, $i ] ) ->pack(-side=>'left'); } MainLoop; sub Sing_msg { my $key = $_[0]; $msg = "♪ ".$doremi{$key}." ♪"; } #---- #doremi.pl