リスト : Packer のリサイズ use strict; use warnings; use Tkx; my $top = Tkx::widget->new('.'); Tkx::option_add('*font', ['', 12]); $top->new_button(-text => 'button 0')->g_pack(-expand => 1, -fill => 'both'); $top->new_button(-text => 'button 1')->g_pack(-expand => 1, -fill => 'both'); Tkx::MainLoop();
Packer によるボタンの配置
ウィンドウを拡大する
関数呼び出し Tkx::grid_columnconfigure($window, column_index, options); Tkx::grid_rowconfigure($window, row_index, options); メソッド呼び出し $window->g_grid_columnconfigure(column_index, options); $window->g_grid_rowconfigure(row_index, options);
-minsize | 最小の幅/高さを数値で指定する |
-weight | 余白を配分するときの割合を数値で指定する |
-pad | 詰め物を数値で指定する |
リスト : Gridder のリサイズ use strict; use warnings; use Tkx; my $top = Tkx::widget->new('.'); Tkx::option_add('*font', ['', 12]); my @column_data = (0, 0, 1, 1); my @row_data = (0, 1, 0, 1); foreach my $i (0 .. 3) { $top->new_button(-text => "button $i") ->g_grid(-column => $column_data[$i], -row => $row_data[$i], -sticky => 'nsew'); } Tkx::MainLoop();
Gridder によるボタンの配置
$top->g_grid_columnconfigure(0, -weight => 1);
button 0, 1 は横方向に伸びる
$top->g_grid_columnconfigure(1, -weight => 2);
4 つのボタンが横方向に伸びる
$top->g_grid_rowconfigure(0, -weight => 1); $top->g_grid_rowconfigure(1, -weight => 2);
4 つのボタンが縦横方向に伸びる
リスト : キャンバスウィジェットのリサイズ (1) use strict; use warnings; use Tkx; # メインウィンドウの設定 my $top = Tkx::widget->new('.'); # キャンバスの設定 my $c0 = $top->new_canvas(-bg => 'darkgreen', -width => 200, -height => 200); $c0->create_rectangle(20, 20, 180, 180, -fill => 'red'); $c0->g_pack(-fill => 'both', -expand => 1); Tkx::MainLoop();
キャンバスウィジェットを配置
ウィンドウを縮小
ウィンドウを拡大
$top->g_bind('<Configure>', change_size)
リスト : キャンバスウィジェットのリサイズ (2) use strict; use warnings; use Tkx; # メインウィンドウの設定 my $top = Tkx::widget->new('.'); $top->g_wm_minsize(100, 100); $top->g_wm_maxsize(400, 400); # キャンバスの設定 my $c0 = $top->new_canvas(-bg => 'darkgreen', -width => 200, -height => 200); $c0->g_pack(-fill => 'both', -expand => 1); my $id = $c0->create_rectangle(20, 20, 180, 180, -fill => 'red'); # 図形の大きさを変更 sub change_size { my $w = $c0->g_winfo_width; my $h = $c0->g_winfo_height; $c0->coords($id, 20, 20, $w - 20, $h - 20); } # バインドの設定 $top->g_bind("<Configure>", \&change_size); Tkx::MainLoop();
ウィンドウを縮小
ウィンドウを拡大
リスト : フレームの表示 use strict; use warnings; use Tkx; my $top = Tkx::widget->new('.'); foreach my $r ('raised', 'sunken', 'flat', 'groove', 'ridge') { $top->new_frame(-width => 60, -height => 40, -relief => $r, -borderwidth => 4, -bg => 'gray')->g_pack(-padx => 5, -pady => 5, -side=> 'left'); } Tkx::MainLoop();
フレームの形状
リスト : フレームにボタンを配置する use strict; use warnings; use Tkx; my $top = Tkx::widget->new('.'); Tkx::option_add('*font', ['', 12]); # フレームの生成 my $f0 = $top->new_frame(); my $f1 = $top->new_frame(); # フレーム $f0 にボタンを配置 $f0->new_button(-text => "button 00")->g_pack(-side => 'left'); $f0->new_button(-text => "button 01")->g_pack(-side => 'left'); $f0->new_button(-text => "button 02")->g_pack(-side => 'left'); # フレーム $f1 にボタンを配置 $top->new_button(-text => "button 10")->g_pack(-in => $f1, -fill => 'both'); $top->new_button(-text => "button 20")->g_pack(-in => $f1, -fill => 'both'); $top->new_button(-text => "button 30")->g_pack(-in => $f1, -fill => 'both'); # フレームを配置する $f0->g_pack(); $f1->g_pack(-fill => 'both'); Tkx::MainLoop();
フレームを使ってボタンを配置する
$widget->g_raise(widget1); $widget->g_lower(widget1);
リスト : フレームの下にボタンを隠す use strict; use warnings; use Tkx; my @button = (); # ボタンを隠す sub hide { my $i = shift; $button[$i]->g_lower; } # メインウィンドウ my $top = Tkx::widget->new('.'); Tkx::option_add('*font', ['', 12]); # フレーム my $frame = $top->new_frame(); # ボタン $top->new_button(-text => 'Show', -command => sub { $frame->g_lower; }) ->g_pack(-in => $frame, -fill => 'x'); foreach my $i (1 .. 4) { $button[$i] = $top->new_button(-text => "button $i", -command => [\&hide, $i]); $button[$i]->g_pack(-in => $frame, -fill => 'x'); } $frame->g_pack(-fill => 'x'); Tkx::MainLoop();
button 1 と 3 を押した状態
Show を押した状態
リスト : ラベルフレーム (1) use strict; use warnings; use Tkx; my $top = Tkx::widget->new('.'); foreach my $r ('nw', 'n', 'ne', 'sw', 's', 'se') { my $f = $top->new_labelframe(-text => 'label', -width => 80, -height => 60, -labelanchor => $r); $f->g_pack(-padx => 5, -pady => 5, -side => 'left'); } Tkx::MainLoop();
ラベルフレーム(1)
リスト : ラベルフレーム (2) use strict; use warnings; use Tkx; my $top = Tkx::widget->new('.'); foreach my $r ('raised', 'sunken', 'flat', 'groove', 'ridge') { $top->new_labelframe(-text => 'label', -width => 80, -height => 60, -relief => $r, -borderwidth => 4, -bg => 'gray')->g_pack(-padx => 5, -pady => 5, -side=> 'left'); } Tkx::MainLoop();
ラベルフレーム(2)
リスト : ラベルフレーム (3) use strict; use warnings; use Tkx; my $top = Tkx::widget->new('.'); my $v = 0; my $f0 = $top->new_labelframe(-text => 'Group1'); my $f1 = $top->new_labelframe(-text => 'Group2'); foreach my $x (0, 1, 2) { $f0->new_radiobutton(-text => "radiobutton $x", -value => $x, -variable => \$v)->g_pack; $f1->new_checkbutton(-text => "checkbutton $x")->g_pack; } $f0->g_pack(-padx => 5, -pady => 5, -side => 'left'); $f1->g_pack(-padx => 5, -pady => 5, -side => 'left'); Tkx::MainLoop();
ラベルフレーム(3)
リスト : ラベルフレーム (4) use strict; use warnings; use Tkx; my $top = Tkx::widget->new('.'); Tkx::option_add('*font', ['', 12]); # 値を格納するオブジェクト my $flag = 0; my $v = 0; # ラジオボタンを格納 my @buttons = (); # ボタンの状態を変更 sub change_state { my $new_state; if ($flag) { $new_state = 'normal'; } else { $new_state = 'disabled'; } foreach $b (@buttons) { $b->configure(-state => $new_state); } } # チェックボタン my $cb = $top->new_checkbutton(-text => 'use button', -variable => \$flag, -command => \&change_state); # ラベルフレーム my $f = $top->new_labelframe(-labelwidget => $cb); # ラジオボタン foreach my $x (0, 1, 2) { my $b = $f->new_radiobutton(-text => "radiobutton $x", -value => $x, -variable => \$v, -state => 'disabled'); $b->g_pack; $buttons[$x] = $b; } # フレームの配置 $f->g_pack(-padx => 5, -pady => 5); Tkx::MainLoop();
disabled
normal
リスト:ペインドウィンドウ (1) use strict; use warnings; use Tkx; my $top = Tkx::widget->new('.'); Tkx::option_add('*font', ['', 12]); # ペインドウィンドウの生成 my $pw = $top->new_panedwindow(-sashwidth => 4); $pw->g_pack(-expand => 1, -fill => 'both'); # ラベルの生成 my $a = $pw->new_label(-text => "panedwindow\ntest1", -bg => 'white'); my $b = $pw->new_label(-text => "panedwindow\ntest2", -bg => 'yellow'); # ペインドウィンドウに追加 $pw->add($a); $pw->add($b); Tkx::MainLoop();
1. 2.
3.
リスト : ペインドウィンドウ (2) use strict; use warnings; use Tkx; my $top = Tkx::widget->new('.'); Tkx::option_add('*font', ['', 12]); # ペインドウィンドウの生成 my $pw = $top->new_panedwindow(-orient => 'vertical', -sashwidth => 4); $pw->g_pack(-expand => 1, -fill => 'both'); # ラベルの生成 my $a = $pw->new_label(-text => "panedwindow\ntest1", -bg => 'white'); my $b = $pw->new_label(-text => "panedwindow\ntest2", -bg => 'yellow'); my $c = $pw->new_label(-text => "panedwindow\ntest3", -bg => 'cyan'); # ペインドウィンドウに追加 $pw->add($a); $pw->add($b); $pw->add($c); Tkx::MainLoop();
1.
2.
3.
リスト : ペインドウィンドウ (3) use strict; use warnings; use Tkx; my $top = Tkx::widget->new('.'); Tkx::option_add('*font', ['', 12]); my @la = (); my @var = (1, 1, 1, 1); # ペインドウィンドウの生成 my $pw = $top->new_panedwindow(-orient => 'vertical', -sashwidth => 4); $pw->g_pack(-expand => 1, -fill => 'both'); # フレーム my $f = $pw->new_frame; $pw->add($f); # ラベルの表示切り替え sub change_label { my $n = shift; if ($var[$n]) { $pw->add($la[$n]); } else { $pw->forget($la[$n]); } } # チェックボタン foreach my $x (0, 1, 2, 3) { $f->new_checkbutton(-text => "display label $x", -variable => \$var[$x], -command => [\&change_label, $x])->g_pack; } # ラベル my @colors = ('white', 'yellow', 'cyan', 'pink'); foreach my $x (0, 1, 2, 3) { my $a = $pw->new_label(-text => "panedwindow\ntest$x", -bg => $colors[$x]); $la[$x] = $a; $pw->add($a); } Tkx::MainLoop();
1.
2.
3.
リスト:ペインドウィンドウ (4) use strict; use warnings; use Tkx; my $top = Tkx::widget->new('.'); Tkx::option_add('*font', ['', 12]); # ペインドウィンドウ 1 my $pw1 = $top->new_panedwindow; $pw1->g_pack(-expand => 1, -fill => 'both'); # ラベル 1 my $a =$pw1-> new_label(-text => "panedwindow\ntest1", -bg => 'yellow'); # ペインドウィンドウ 2 my $pw2 = $top->new_panedwindow(-orient => 'vertical'); # ラベル 2, 3 my $b = $pw2->new_label(-text => "panedwindow\ntest2", -bg => 'cyan'); my $c = $pw2->new_label(-text => "panedwindow\ntest3", -bg => 'pink'); # ペインドウィンドウに配置 $pw1->add($a); $pw1->add($pw2); $pw2->add($b); $pw2->add($c); Tkx::MainLoop();
1.
2.
3.