リスト : 式入力電卓
use strict;
use warnings;
use Safe;
use Tkx;
# 設定
Tkx::option_add('*font', ['', 14]);
my $buffer = "";
my $cpt = new Safe;
# 計算
sub calc {
$buffer = $cpt->reval($buffer);
}
my $top = Tkx::widget->new('.');
my $e0 = $top->new_entry(-textvariable => \$buffer);
$e0->g_pack;
$e0->g_focus;
$e0->g_bind("<Return>", \&calc); # バインディング
Tkx::MainLoop();
$widget->g_focus;
数式を入力する
リターンキーで計算する
$widget->g_bind(eventsequence, callback);
<modifier-modifier-type-detail>
| Key, KeyPress | キーが押された |
| KeyRelease | キーが離された |
| Button, ButtonPress | マウスのボタンが押された |
| ButtonRelease | マウスのボタンが離された |
| Motion | マウスの移動 |
| Enter | マウスカーソルがウィンドウの中に入った |
| Leave | マウスカーソルがウィンドウから出た |
| Control | Ctrl キーを押しながらの入力 |
| Shift | Shift キーを押しながらの入力 |
| Alt | Alt キーを押しながらの入力 |
| Button1, B1 | マウスの左ボタンを押しながらの入力 |
| Button3, B3 | マウスの右ボタンを押しながらの入力 |
| Double | ダブルクリック |
| Triple | トリプルクリック |
| %b | マウスボタンの番号 |
| %x, %y | マウスカーソルの座標 |
| %t | イベントの発生時刻 |
| %A | キーに対応する文字 |
| %K | キーに対応する名前 |
リスト : キーの名前を表示
use strict;
use warnings;
use Tkx;
Tkx::option_add('*font', ['', 14]);
my $top = Tkx::widget->new('.');
my $buffer = "";
# キーの表示
sub print_key {
my $key = shift;
$buffer = "push key is $key";
}
$top->new_label(-text => '*** push any key ***')->g_pack;
my $l = $top->new_label(-textvariable => \$buffer);
$l->g_pack(-anchor => 'w');
$l->g_bind("<Any-KeyPress>", [\&print_key, Tkx::Ev("%K")] );
$l->g_focus;
Tkx::MainLoop();
F1 キーを押したときの動作
| -xscrollcommand | x 方向のスクロールメソッドを指定 |
| -yscrollcommand | y 方向のスクロールメソッドを指定 |
| -selectmode | セレクションモード |
リスト : 色の選択
use strict;
use warnings;
use Tkx;
Tkx::option_add('*font', ['', 12]);
my $top = Tkx::widget->new('.');
my $buff = "";
# ラベルの生成
my $la = $top->new_label(-textvariable => \$buff);
$la->g_pack(-fill => 'x');
# リストボックスの生成
my $lb = $top->new_listbox;
$lb->g_pack;
# 色の選択
sub get_color {
my @xs = Tkx::SplitList($lb->curselection());
if (@xs) {
my $color = $lb->get($xs[0]);
$buff = $color;
$la->configure(-bg => $color);
}
}
# バインディング
$lb->g_bind('<ButtonRelease-1>', \&get_color);
foreach my $x ('red', 'green', 'blue', 'yellow', 'cyan', 'pink', 'white', 'black') {
$lb->insert('end', $x);
}
Tkx::MainLoop();
初期状態
yellow を選択
pink を選択
| -orient | スクロールバーの方向 |
| -troughcolor | 矢印とスクロールの隙間の色 |
| -command | スクロールバーが動いたときに実行するメソッド |
-xscrollcommand => [$scrollbar_widget, "set"]
-command => [$listbox_widget, "yview"]
リスト : フォントの表示
use strict;
use warnings;
use utf8;
use Tkx;
# メインウィンドウ
my $top = Tkx::widget->new('.');
# ラベルの生成
my $la = $top->new_label(-text => "Hello, world!, こんにちは世界!!", -font => ['', 12]);
$la->g_pack(-fill => 'x');
# リストボックスの生成
my $lb = $top->new_listbox(-selectmode => 'single', -height => 20, -width => 40);
$lb->g_pack(-side => 'left');
# スクロールバーの生成
my $sb = $top->new_scrollbar(-command => [$lb, 'yview']);
$sb->g_pack(-side => 'left', -fill => 'y');
$lb->configure(-yscrollcommand => [$sb, 'set']);
# フォントの選択
sub get_font {
my @xs = Tkx::SplitList($lb->curselection());
if (@xs) {
my $font = $lb->get($xs[0]);
$la->configure(-font => [$font, 12]);
}
}
# バインディング
$lb->g_bind('<<ListboxSelect>>', \&get_font);
my @name_list = Tkx::SplitList(Tkx::font_families());
foreach my $name (sort @name_list) {
$lb->insert('end', $name);
}
Tkx::MainLoop();
初期状態
Consolas を選択
Ricty Diminished を選択
メイリオを選択
| n | 上寄せ |
| s | 下寄せ |
| e | 右寄せ |
| w | 左寄せ |
| ns | 上下方向に引き伸ばす |
| ew | 左右方向に引き伸ばす |
リスト : ボタンを格子状に配置
use strict;
use warnings;
use Tkx;
my $top = Tkx::widget->new('.');
foreach my $x (0 .. 4) {
foreach my $y (0 .. 4) {
my $n = $y * 5 + $x + 1;
$top->new_button(-text => " $n ")->g_grid(-column => $x, -row => $y, -sticky => 'ew');
}
}
Tkx::MainLoop();

リスト : 履歴付き式入力電卓
use strict;
use warnings;
use Safe;
use Tkx;
my $buffer = ""; # グローバル変数
my $cpt = new Safe;
my $top = Tkx::widget->new('.');
Tkx::option_add('*font', ['', 14]);
# Entry
my $e0 = $top->new_entry(-textvariable => \$buffer);
# Listbox
my $lb = $top->new_listbox;
# Scrollbar
my $sb1 = $top->new_scrollbar(-orient => 'v', -command => [$lb, 'yview']);
my $sb2 = $top->new_scrollbar(-orient => 'h', -command => [$lb, 'xview']);
# Listbox の設定
$lb->configure(-yscrollcommand => [$sb1, 'set']);
$lb->configure(-xscrollcommand => [$sb2, 'set']);
# grid による配置
$e0->g_grid(-row => 0, -columnspan => 2, -sticky => 'ew');
$lb->g_grid(-row => 1, -column => 0, -sticky => 'nsew');
$sb1->g_grid(-row => 1, -column => 1, -sticky => 'ns');
$sb2->g_grid(-row => 2, -column => 0, -sticky => 'ew');
# 計算
sub calc {
$lb->insert('end', $buffer);
$lb->see('end');
$buffer = $cpt->reval($buffer);
$e0->icursor(0);
}
# 取り出し
sub get_expr {
$buffer = $lb->get('active');
$e0->g_focus;
}
# バインディング
$e0->g_bind("<Return>", \&calc);
$lb->g_bind("<Double-1>", \&get_expr);
# フォーカスの設定
$e0->g_focus;
Tkx::MainLoop();
式の履歴が残るように改造した電卓
リターンキーを押せば計算結果も履歴に入る
式の選択
リスト : チェックボタンとラジオボタン
use strict;
use warnings;
use Tkx;
my $top = Tkx::widget->new('.');
Tkx::option_add('*font', ['', 14]);
my $opts1 = 1;
my $opts2 = 0;
my $opts3 = 1;
my $action = 1;
$top->new_label(-text => 'Check Button')->g_pack;
$top->new_checkbutton(-text => 'option 1', -variable => \$opts1)->g_pack;
$top->new_checkbutton(-text => 'option 2', -variable => \$opts2)->g_pack;
$top->new_checkbutton(-text => 'option 3', -variable => \$opts3)->g_pack;
$top->new_label(-text => 'Radio Button')->g_pack;
$top->new_radiobutton(-text => 'action A', -variable => \$action, -value => 0)->g_pack;
$top->new_radiobutton(-text => 'action B', -variable => \$action, -value => 1)->g_pack;
$top->new_radiobutton(-text => 'action C', -variable => \$action, -value => 2)->g_pack;
Tkx::MainLoop();
チェックボタンとラジオボタン
| 関数名 | 機能 |
|---|---|
| image_create_photo(名前, -file => ファイル名); | 画像ファイルからイメージを生成する |
| image_delete(名前); | イメージを削除する |
| image_names(); | 全イメージの名前をリストにして返す |
| image_height(名前); | イメージの高さを返す |
| image_weight(名前); | イメージの幅を返す |
リスト : 画像の表示
use strict;
use warnings;
use Tkx;
my $top = Tkx::widget->new('.');
my $image1 = Tkx::image_create_photo(-file => 'earth.gif');
$top->new_label(-image => $image1)->g_pack;
Tkx::MainLoop();
earth.gif の表示
-filetypes: " ファイル種別 ... "
ファイル種別 := {名前 {拡張子 ... }}
-filetypes => "{{画像 Files} {.gif .ppm}}"
-filetypes => "{GIF {.gif}} {PPM {.ppm}} {ALL {*}}"
リスト : 画像ローダー
use strict;
use warnings;
use Tkx;
use File::Basename;
my $top = Tkx::widget->new('.');
Tkx::option_add("*tearOff", 0);
my $m = $top->new_menu(-type => 'menubar');
$top->configure(-menu => $m);
my $path_name = "."; # パスを格納する変数
my $image_data = Tkx::image_create_photo(-width => 64, -height => 64);
my $label = $top->new_label(-image => $image_data);
$label->g_pack;
# ファイルを選んで表示する
sub load_file {
my $filename = Tkx::tk___getOpenFile(-filetypes => "{GIF {.gif}} {PPM {.ppm}} {ALL {*}}",
-initialdir => $path_name );
if ($filename) {
$path_name = dirname($filename) . '/';
Tkx::image_delete($image_data);
$image_data = Tkx::image_create_photo(-file => $filename);
$label->configure(-image => $image_data);
}
}
my $m1 = $m->new_menu;
$m->add_cascade(-menu => $m1, -label => 'File', -under => 0);
$m1->add_command(-label => 'Open', -under => 0, -command => \&load_file);
$m1->add_separator;
$m1->add_command(-label => 'Exit', -under => 0, -command => sub { exit; });
Tkx::MainLoop();
GIF ファイルの選択
PPM ファイルの選択
PPM ファイルを表示