Ruby/TkではTkLabelクラスが相当します.
使用例を示します.
#!/usr/local/bin/ruby require 'tk' label1 = TkLabel.new(nil, 'text' => '文字列ラベルの例です.') label1.pack('side' => 'top', 'fill' => 'both') label2 = TkLabel.new(nil, 'text' => 'インスタンスをを様々に設定したラベルの例です.長いラベルももちろん書けます.2行以上にわたらせる場合はwraplengthインスタンスを設定しておきましょう.もっともこんな場合はmessageウィジェットを使う方が効果的です.', 'anchor' => 'se', 'relief' => 'raised', 'borderwidth' => 10, 'background' => 'darkblue', 'foreground' => 'white', 'justify' => 'left', 'wraplength' => 200, 'height' => 10, 'width' => 50) label2.pack('side' => 'top', 'fill' => 'both') label3 = TkLabel.new(nil, 'background' => 'green4', 'image' => TkPhotoImage.new('file'=>'~kajiyama/WWW/fig/portrait.gif')) label3.pack('side' => 'left') quitbutton = TkButton.new(nil, 'text' => 'quit', 'command' => proc{exit}) quitbutton.pack('side' => 'bottom', 'fill' => 'both') Tk.mainloop上記のプログラムを走らせると,下のようになります.
TkLabelクラスのインスタンス変数を以下に示します.
インスタンス変数名 | 役割 |
---|---|
background | 背景色. |
foreground | 前景色. |
anchor | 配置場所.'n','e','s','w','center'等で指定. |
bitmap | 表示するbitmap.TkBitmapImageオブジェクトで指定. |
image | 表示するimage.TkPhotoImageオブジェクトで指定. |
text | 表示する文字列. |
textvariable | 表示する文字列変数.TkVariableオブジェクトで指定. |
justify | 文字列をよせる側."left","center","right"で指定. |
borderwidth | 枠の幅.整数値で指定. |
cursor | マウスカーソルの形. |
highlightcolor | ウィジェットにフォーカスがある時の枠の色. |
highlightthickness | ウィジェットにフォーカスがある時の枠の幅. |
relief | ウィジェットの凹凸.'raised'または'sunken'で指定. |
wraplength | 折り畳むときの1行の文字数. |
height | ウィジェットの高さ. |
width | ウィジェットの幅. |