| オプション | 機能 | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| -type |
| ||||||||||||
| -message | ダイアログに表示する文字列 | ||||||||||||
| -icon | ダイアログに表示するアイコン (error, info, question, warning のいずれかを指定) | ||||||||||||
| -default | デフォルトボタンの名前を指定 | ||||||||||||
| -title | ダイアログのタイトルを指定 | ||||||||||||
| -parent | ダイアログを表示するウィンドウを指定 |
リスト : tk___messagebox のサンプル
use strict;
use warnings;
use utf8;
use Tkx;
my $icon = 'info';
my $type = 'ok';
# 画面の設定
my $top = Tkx::widget->new('.');
Tkx::option_add('*font', ['', 14]);
Tkx::option_add('*tearOff', 0);
# ウィンドウの生成
sub message_window {
Tkx::tk___messageBox(-type => $type, -icon => $icon, -title => 'About',
-message => 'tk___messageBox のサンプルプログラムです');
}
# メニューの設定
my $m = $top->new_menu(-type => 'menubar');
$top->configure(-menu => $m);
$m->add_command(-label => 'About', -under => 0, -command => \&message_window);
my $m1 = $m->new_menu;
$m->add_cascade(-menu => $m1, -label => 'Type', -under => 0);
my $m2 = $m->new_menu;
$m->add_cascade(-menu => $m2, -label => 'Icon', -under => 0);
# メッセージボックスのボタンを指定するメニュー
$m1->add_radiobutton(-label => 'ok',
-variable => \$type, -value => 'ok');
$m1->add_radiobutton(-label => 'ok, cancel',
-variable => \$type, -value => 'okcancel');
$m1->add_radiobutton(-label => 'yes, no',
-variable => \$type, -value => 'yesno');
$m1->add_radiobutton(-label => 'yes, no, cancel',
-variable => \$type, -value => 'yesnocancel');
$m1->add_radiobutton(-label => 'retry, cancel',
-variable => \$type, -value => 'retrycancel');
$m1->add_radiobutton(-label => 'abort, retry, ignore',
-variable => \$type, -value => 'abortretryignore');
# メッセージボックスのアイコンを指定するメニュー
$m2->add_radiobutton(-label => 'info', -variable => \$icon, -value => 'info');
$m2->add_radiobutton(-label => 'error', -variable => \$icon, -value => 'error');
$m2->add_radiobutton(-label => 'question', -variable => \$icon, -value => 'question');
$m2->add_radiobutton(-label => 'warning', -variable => \$icon, -value => 'warning');
$top->new_label(-text => 'メニュー About を選んでね')->g_pack;
Tkx::MainLoop();
メインウィンドウの画像
メッセージボックス (info, ok) の画像
メッセージボックス (warning, yes/no) の画像
リスト : キャンバスウィジェット
use strict;
use warnings;
use Tkx;
my $top = Tkx::widget->new('.');
my $c0 = $top->new_canvas(-width => 150, -height => 150);
$c0->g_pack;
Tkx::MainLoop();
空のキャンバス
| create_line() | 直線(折れ線) |
| create_oval() | 楕円 |
| create_arc() | 円弧(楕円の円周の一部) |
| create_rectangle() | 矩形 |
| create_polygon() | 多角形 |
| create_image() | イメージ |
| create_bitmap() | ビットマップ |
| create_text() | 文字列 |
| create_window() | 任意のウィジェット |
リスト : 楕円の描画
use strict;
use warnings;
use Tkx;
my $top = Tkx::widget->new('.');
my $c0 = $top->new_canvas(-width => 150, -height => 150);
my $id = $c0->create_oval(10, 10, 140, 140);
# $c0->itemconfigure($id, -fill => 'red');
$c0->g_pack;
Tkx::MainLoop();
楕円の描画
楕円の描画 (塗りつぶし)
リスト : 矩形の描画
use strict;
use warnings;
use Tkx;
my $top = Tkx::widget->new('.');
my $c0 = $top->new_canvas(-width => 150, -height => 150);
$c0->create_rectangle(10, 10, 140, 140, -fill => 'green');
$c0->g_pack;
Tkx::MainLoop();
矩形の描画 (stipple なし)
矩形の描画 (-stipple => 'gray25' を追加)
リスト : 直線の描画
use strict;
use warnings;
use Tkx;
my $top = Tkx::widget->new('.');
my $c0 = $top->new_canvas(-width => 150, -height => 150);
my $id = $c0->create_line(10, 10, 140, 10, 10, 140, 140, 140);
$c0->g_pack;
Tkx::MainLoop();
直線の描画
直線の色と太さを変更
直線の描画(-smooth => 1)
リスト : 多角形の描画
use strict;
use warnings;
use Tkx;
my $top = Tkx::widget->new('.');
my $c0 = $top->new_canvas(-width => 150, -height => 150);
my $id = $c0->create_polygon(75, 10, 140, 70, 110, 140, 40, 140, 10, 70, -smooth => 1);
$c0->g_pack;
Tkx::MainLoop();
多角形の描画
多角形の描画 (-smooth => 1)
リスト : 円弧の描画
use strict;
use warnings;
use Tkx;
my $top = Tkx::widget->new('.');
my $c0 = $top->new_canvas(-width => 400, -height => 200);
my $r = 0;
foreach my $c ('red', 'blue', 'green', 'pink', 'cyan', 'yellow',
'magenta', 'brown', 'orange', 'white', 'gray', 'black') {
$c0->create_arc(10, 10, 390, 190, -start => $r, -extent => 30, -fill => $c);
$r += 30;
}
$c0->g_pack;
Tkx::MainLoop();
円弧の描画
リスト : イメージの表示
use strict;
use warnings;
use Tkx;
my $top = Tkx::widget->new('.');
my $c0 = $top->new_canvas(-width => 400, -height => 300);
my $image = Tkx::image_create_photo(-file => 'earth.gif');
$c0->create_image(200, 150, -image => $image);
$c0->g_pack;
Tkx::MainLoop();
$c0->create_image(x, y, オプション, ... ) $c0->create_bitmap(x, y, オプション, ... )
イメージの描画
リスト : テキストの表示
use strict;
use warnings;
use Tkx;
my $top = Tkx::widget->new('.');
my $c0 = $top->new_canvas(-width => 150, -height => 150);
my $id = $c0->create_text(75, 75, -text => 'hello, world!', -font => ['', 14]);
$c0->g_pack;
Tkx::MainLoop();
$c0->create_text(x, y, オプション, ...);
テキストの描画
リスト : ラベルウィジェットの表示
use strict;
use warnings;
use Tkx;
my $top = Tkx::widget->new('.');
my $c0 = $top->new_canvas(-width => 150, -height => 150);
my $label = $top->new_label(-text => 'hello, world!', -bg => 'green', -font => ['', 14]);
$c0->create_window(75, 75, -window => $label);
$c0->g_pack;
Tkx::MainLoop();
$c0->create_window(x, y, オプション, ...)
ラベルの描画
| type(ID) | 図形の種別を返す |
| bbox(ID, ...) | 指定した図形を囲む領域 (矩形) をリストにして返す |
| coords(ID, x0, y0, ...) | 図形の座標の設定や問い合わせ |
| delete(ID, ...) | 図形の削除 |
| move(ID, dx, dt) | 図形の移動 |
| lower(ID1, ID2) | 重なり順を低くする |
| raise(ID1, ID2) | 重なり順を高くする |
| bind(ID, eventsequence, callback) | バインディングの設定 |
リスト : バインディングの設定 (1)
use strict;
use warnings;
use Tkx;
my $top = Tkx::widget->new('.');
my $c0 = $top->new_canvas(-width => 200, -height => 150);
my $id = $c0->create_rectangle(10, 10, 20, 20, -fill => 'brown');
# 移動
sub move_rect {
my ($x, $y) = @_;
$c0->coords($id, $x - 5, $y - 5, $x + 5, $y + 5);
}
# バインディング
$c0->bind($id, "<B1-Motion>" => [\&move_rect, Tkx::Ev('%x', '%y')]);
$c0->g_pack;
Tkx::MainLoop();
初期状態
矩形をドラッグで移動する
リスト : バインディングの設定 (2)
use strict;
use warnings;
use Tkx;
my $top = Tkx::widget->new('.');
my $c0 = $top->new_canvas(-width => 200, -height => 150);
$c0->create_rectangle(10, 10, 20, 20, -fill => 'brown', -tags => 'brown');
$c0->create_rectangle(20, 10, 30, 20, -fill => 'brown', -tags => 'brown');
$c0->create_rectangle(30, 10, 40, 20, -fill => 'brown', -tags => 'brown');
# 移動
sub move_rect {
my ($x, $y) = @_;
$c0->coords('current', $x - 5, $y - 5, $x + 5, $y + 5);
}
# バインディング
$c0->bind('brown', "<B1-Motion>" => [\&move_rect, Tkx::Ev('%x', '%y')]);
$c0->g_pack;
Tkx::MainLoop();
$c0->itemconfigure('brown', -fill => 'green');
$c0->delete('brown');
初期状態
矩形をドラッグで移動する
$sub_win = $top->new_toplevel();
リスト : ウィンドウの生成
use strict;
use warnings;
use utf8;
use Tkx;
my $top = Tkx::widget->new('.');
Tkx::option_add('*font', ['', 14]);
# メッセージの表示
sub message_window {
my $sub_win = $top->new_toplevel();
$sub_win->new_message(-text => 'message のサンプルプログラムです')->g_pack;
}
my $m = $top->new_menu(-type => 'menubar');
$top->configure(-menu => $m);
$m->add_command(-label => 'About', -under => 0, -command => \&message_window);
$top->new_label(-text => 'メニュー About を選んでね')->g_pack;
Tkx::MainLoop();
メインウィンドウ
About をクリックしてサブウィンドウを表示
| winfo_geometry($widget) | ウィジェットの位置を文字列 (幅x高さ+x+y) で返す |
| winfo_width($widget) | ウィジェットの幅を返す |
| winfo_height($widget) | ウィジェットの高さを返す |
| winfo_x($widget) | 親ウィンドウ内での x 座標を返す |
| winfo_y($widget) | 親ウィンドウ内での y 座標を返す |
| winfo_rootx($widget) | ディスプレイ上での x 座標を返す |
| winfo_rooty($widget) | ディスプレイ上での y 座標を返す |
| winfo_exists($widget) | ウィジェットが存在するか |
| withdraw($window) | ウィンドウを画面から取り除く |
| deiconify($window) | ウィンドウを見える状態に戻す |
| iconify($window) | ウィンドウをアイコン化する |
| geometry($window, string) | ウィンドウを表示する位置を文字列で (幅x高さ+x+y) で指定する |
| maxsize($window, 幅, 高さ) | ウィンドウの最大値を指定 |
| minsize($window, 幅, 高さ) | ウィンドウの最小値を指定 |
| title($window, タイトル名) | ウィンドウのタイトルを指定 |
リスト : ウィンドウの生成 (改良版)
use strict;
use warnings;
use utf8;
use Tkx;
my $top = Tkx::widget->new('.');
Tkx::option_add('*font', ['', 14]);
$top->g_wm_title('Main');
# メッセージの表示
my $sub_win;
sub message_window {
unless (Tkx::winfo_exists($sub_win)) {
$sub_win = $top->new_toplevel();
$sub_win->g_wm_title('About');
$sub_win->new_message(-text => 'message のサンプルプログラムです')->g_pack;
}
}
my $m = $top->new_menu(-type => 'menubar');
$top->configure(-menu => $m);
$m->add_command(-label => 'About', -under => 0, -command => \&message_window);
$top->new_label(-text => 'メニュー About を選んでね')->g_pack;
Tkx::MainLoop();
メインウィンドウ (改良版)
サブウィンドウ (改良版, 横に引き延ばしてある)
| normal | 通常の状態 |
| active | アクティブな状態 |
| disabled | 無効な状態 |
| -activeforeground | アクティブ時の色を指定 |
| -activebackground | アクティブ時の背景色を指定 |
| -disabledforeground | 無効時の色を指定 |
リスト : ボタンの状態を変更する
use strict;
use warnings;
use Tkx;
my $var = 'normal';
# メインウィドウ
my $top = Tkx::widget->new('.');
Tkx::option_add('*font', ['', 14]);
# ボタンの設定
my $button = $top->new_button(-text => 'button',
-activeforeground => 'green',
-disabledforeground => 'red');
$button->g_pack(-fill => 'x');
# ボタンの状態を変更
sub change_state {
$button->configure(-state => $var);
}
# ラジオボタンの設定
foreach my $i ('normal', 'active', 'disabled') {
$top->new_radiobutton(-text => $i, -value => $i, -variable => \$var,
-command => \&change_state)->g_pack(-anchor => 'w');
}
Tkx::MainLoop();
ボタンの状態を変更
$menu->entryconfigure(項目, option, value)
| N | 数値で指定 (先頭の項目が 0 番目となる) |
| @N | 画面上端から N ピクセルだけ下にある項目 |
| end, last | 最後の項目 |
| active | アクティブな状態にある項目 |
| none | どれでもない項目 (全ての項目を非アクティブにするために使用する) |
| パターン | パターンと一致するラベル名を持つ項目 |
リスト : メニューの状態を変更する
use strict;
use warnings;
use Tkx;
my $top = Tkx::widget->new('.');
Tkx::option_add('*font', ['', 14]);
Tkx::option_add('*tearOff', 0);
# ダミー
sub dummy { }
# メニューの設定
my $m = $top->new_menu(-type => 'menubar');
$top->configure(-menu => $m);
my $m1 = $m->new_menu;
$m->add_cascade(-label => "Menu", -under => 0, -menu => $m1);
$m1->add_command(-label => "Menu1", -command => \&dummy);
$m1->add_command(-label => "Menu2", -command => \&dummy);
$m1->add_command(-label => "Menu3", -command => \&dummy);
# 初期化
my $var = "normal";
# メニューの状態を変更する
sub change_state {
$m1->entryconfigure('Menu1', -state => $var);
}
# ラジオボタンの設定
$top->new_radiobutton(-text => "normal", -value => 'normal', -variable => \$var, -command => \&change_state)->g_pack;
$top->new_radiobutton(-text => "active", -value => 'active', -variable => \$var, -command => \&change_state)->g_pack;
$top->new_radiobutton(-text => "disable", -value => 'disable', -variable => \$var, -command => \&change_state)->g_pack;
Tkx::MainLoop();
通常のメニュー
disabled に設定 (Menu1 が灰色)