文字列引数での->put
メソッドを使用することにより、単純なウィジェットが作成されます。ラベルは、単一の前景色を持つようにのみ構成できます。必要なものを実現するには、代わりに (読み取り専用のテキスト ウィジェット) を使用できます。次のコードは、ラベル ウィジェットとテキスト ウィジェットを表示しますが、後者は異なる色で表示されます。Tk::Table
Tk::Label
Tk::ROText
use strict;
use Tk;
use Tk::ROText;
my $mw = tkinit;
# The monocolored Label variant
my $l = $mw->Label
(
-text => "First Name\nMYO",
-font => "{sans serif} 12",
)->pack;
# The multicolored ROText variant
my $txt = $mw->ROText
(
-borderwidth => 0, -highlightthickness => 0, # remove extra borders
-takefocus => 0, # make widget unfocusable
-font => "{sans serif} 12",
)->pack;
$txt->tagConfigure
(
'blue',
-foreground => "blue",
-justify => 'center', # to get same behavior as with Tk::Label
);
$txt->tagConfigure
(
'red',
-foreground => "red",
-justify => 'center', # to get same behavior as with Tk::Label
);
$txt->insert("end", "First Name\n", "blue", "MYO", "red");
# a hack to make the ROText geometry the same as the Label geometry
$txt->GeometryRequest($l->reqwidth, $l->reqheight);
MainLoop;
ご覧のとおり、テキスト ウィジェット バリアントを機能させるには、より多くのタイピングが必要です。したがって、このコードをサブルーチンまたはウィジェット クラス (おそらく CPAN の何か?) に抽象化すると便利です。また、テキスト ウィジェットのジオメトリを自分で処理する必要があることにも注意してください。ラベルは、ラベルの内容に合わせて自動的に拡張されます。テキスト ウィジェットは、デフォルトで 80x24 文字のジオメトリを持ち、そのコンテンツに基づいて自動的に縮小または拡張しません。サンプルでは、ハックを使用GeometryRequest
して、同等のラベル ウィジェットと同じジオメトリを強制しました。代わりに、ハードコーディング-width
で問題ないかもしれません。別の解決策として、 /のメソッドを使用してジオメトリを計算する-height
こともできます。bbox()
Tk::Text
Tk::ROText