リストボックス・スクロールバー

■ リストボックスウィジェット

リストボックスウィジェットは,テキストラベルを縦に並べて欄を作るものです.マウスによる選択操作も可能になります.

書式: $child = $parent->Listbox(?options?)

主なオプション

オプション 説明 オプション値の例
-width => x リストボックスのX方向の文字数を指定する. 12, 24
-height => y リストボックスのY方向の文字数を指定する. 12, 24
-selectforeground => color 選択状態時のテキストの色 'red', '#ff0000'
-selectbackground => color 選択状態時の背景の色 'red', '#ff0000'
-selectmode => mode リストボックスの選択モードを指定する. 'single', 'browse', 'multiple', 'extended'

メソッド:

補足:

サンプル: カレントディレクトリのファイルのリストボックスを作ります.


#!/usr/bin/perl -w 

use Tk;

$mw = MainWindow->new;

$lis = $mw->Listbox(-background=> 'white',
                    -selectforeground=> 'brown',
                    -selectbackground=> 'cyan',
                    -selectmode=> 'extended')
  ->pack(-fill=>'both',
         -expand => 'yes');

foreach $fn (<*>) {
  $lis->insert('end',$fn);
}

$mw->Button(-text=>"表示",
            -command=> \&Print_selection)
  ->pack(-fill=>'x');

MainLoop;

sub Print_selection {

  my @ind = $lis->curselection;
  foreach $i (@ind) {
    printf "%s が選択されています.", $lis->get($i);
  }

}
#---
# listbox.pl

■ スクロールドウィジェット

スクロールバーは表示範囲の限られた場所に,多くの情報を表示させるときに必須のものです.Scrollbarというスクロールバーだけを作る専用のウィジェットがありますが,ここでは,スクロールバーとそれとバインドするウィジェットとを,一気に定義できるScrolled というものを紹介します.

書式: $child = $parent->Scrolled(Whatever, -scrollbars=> where, ?options?)

主なオプション

オプション 説明 オプション値の例
-scrollbars => where スクロールバーをつける場所を指定.'w'か'e', 'n'か's'で指定する.oを付けると,必要に応じて自動的にスクロールバーがつく. 's', 'se', 'oe', 'os', 'osoe'

サンプル: 先ほどのサンプルにスクロールバーをつけてみましょう.


#!/usr/bin/perl -w 

use Tk;

$mw = MainWindow->new;

$lis = $mw->Scrolled('Listbox',
                    -scrollbars=> 'osoe',
                    -height => 4,
                    -background=> 'white',
                    -selectforeground=> 'brown',
                    -selectbackground=> 'cyan',
                    -selectmode=> 'extended')
  ->pack(-fill=>'both',
         -expand => 'yes');

foreach $fn (<*>) {
  $lis->insert('end',$fn);
}

$mw->Button(-text=>"表示",
            -command=> \&Print_selection)
  ->pack(-fill=>'x');

MainLoop;

sub Print_selection {

  my @ind = $lis->curselection;
  foreach $i (@ind) {
    printf "%s が選択されています.", $lis->get($i);
  }

}
#---
# listbox_scr.pl