Previous
Radiobutton, Checkbutton 
Next
Menubutton, Menu 
Perl/Tk Tutorial - Create GUI with Perl's Tk Module
Listbox

Widgets 4 : Listbox

Listbox

A listbox is a widget that displays a list of strings, one per line. When first created, a new listbox has no elements. Elements may be added or deleted using widget commands described below.

Some Options
-selectmode => MODE Specifies one of several styles for manipulating the selection. The MODE may be arbitrary, but the default bindings expect it to be either single, browse, multiple, or extended; the default value is browse.


Some Commands
SyntaxDescriptionExample
$widget -> curselection(); Returns a list containing the numerical indices of all of the elements in the listbox that are currently selected. If there are no elements selected in the listbox then an empty string is returned. $sel = $lst -> curselection();
$widget -> delete(first,?last?); Deletes one or more elements of the listbox. First and last are indices specifying the first and last elements in the range to delete. If last isn't specified it defaults to first, i.e. a single element is deleted. $lst -> delete(5);
$widget -> get(first,?last?); If last is omitted, returns the contents of the listbox element indicated by first, or an empty string if first refers to a non-existent element. If last is specified, the command returns a list whose elements are all of the listbox elements between first and last, inclusive. $lst -> get(5,end);
$widget -> index(index); Returns the integer index value that corresponds to index. If index is end the return value is a count of the number of elements in the listbox (not the index of the last element). $lst -> index(5);
$widget -> insert(index,?element element ...?); Inserts zero or more new elements in the list just before the element given by index. If index is specified as end then the new elements are added to the end of the list. Returns an empty string. $lst -> insert('end',"me");
$widget -> size(); Returns a decimal string indicating the total number of elements in the listbox. $count = $lst -> size();


Example

#!/usr/local/bin/perl
use Tk;

#Global Variables
my $age = 10;
my $occupied = 1;
my $gender = "Male";

# Main Window
my $mw = new MainWindow;

#GUI Building Area
my $frm_name = $mw -> Frame();
my $lab = $frm_name -> Label(-text=>"Name:");
my $ent = $frm_name -> Entry();
#Age
my $scl = $mw -> Scale(-label=>"Age :",
	 -orient=>'v',	 	-digit=>1,
	 -from=>10,			-to=>50,
	 -variable=>\$age,	-tickinterval=>10);

#Jobs
my $frm_job = $mw -> Frame();
my $chk = $frm_job -> Checkbutton(-text=>"Occupied",
	-variable=>\$occupied);
$chk -> deselect();
my $lst = $frm_job -> Listbox(-selectmode=>'single');

#Adding jobs
$lst -> insert('end',"Student","Teacher","Clerk","Business Man",
	"Militry Personal","Computer Expert","Others");

#Gender
my $frm_gender = $mw -> Frame();
my $lbl_gender = $frm_gender -> Label(-text=>"Sex ");
my $rdb_m = $frm_gender -> Radiobutton(-text=>"Male",  
		-value=>"Male",  -variable=>\$gender);
my $rdb_f = $frm_gender -> Radiobutton(-text=>"Female",
		-value=>"Female",-variable=>\$gender);

my $but = $mw -> Button(-text=>"Push Me", -command =>\&push_button);

#Text Area
my $textarea = $mw -> Frame();
my $txt = $textarea -> Text(-width=>40, -height=>10);
my $srl_y = $textarea -> Scrollbar(-orient=>'v',-command=>[yview => $txt]);
my $srl_x = $textarea -> Scrollbar(-orient=>'h',-command=>[xview => $txt]);
$txt -> configure(-yscrollcommand=>['set', $srl_y], 
		-xscrollcommand=>['set',$srl_x]);

#Geometry Management
$lab -> grid(-row=>1,-column=>1);
$ent -> grid(-row=>1,-column=>2);
$scl -> grid(-row=>2,-column=>1);
$frm_name -> grid(-row=>1,-column=>1,-columnspan=>2);

$chk -> grid(-row=>1,-column=>1,-sticky=>'w');
$lst -> grid(-row=>2,-column=>1);
$frm_job -> grid(-row=>2,-column=>2);

$lbl_gender -> grid(-row=>1,-column=>1);
$rdb_m -> grid(-row=>1,-column=>2);
$rdb_f -> grid(-row=>1,-column=>3);
$frm_gender -> grid(-row=>3,-column=>1,-columnspan=>2);

$but -> grid(-row=>4,-column=>1,-columnspan=>2);

$txt -> grid(-row=>1,-column=>1);
$srl_y -> grid(-row=>1,-column=>2,-sticky=>"ns");
$srl_x -> grid(-row=>2,-column=>1,-sticky=>"ew");
$textarea -> grid(-row=>5,-column=>1,-columnspan=>2);

MainLoop;

## Functions
#This function will be executed when the button is pushed
sub push_button {
	my $name = $ent -> get();
	$txt -> insert('end',"$name\($gender\) is $age years old and is ");
	
	my $job = "";
	#See whether he is employed
	if ( $occupied == 1 ) {
		my $job_id = $lst -> curselection(); #Get the no of selected jobs
		if ( $job_id eq "" ) { #If there is no job
			$job = "a Non worker.";
		}
		else {
			$job = $lst -> get($job_id) ;#Get the name of the job
			$txt -> insert('end',"a $job.");
		}
	}
	else {
		$txt -> insert('end',"unemployed.");
	}
}

Wow! Our 'little' example is a big (and utterly pointless) program now. I am going to stop 'exampling' from now on.
This is quite complicated isn't it? Why don't you run the script and see what a beautiful script we made. Copy the above script and paste it in a file called "info.pl" and double click the file. Voila! We are Perl/Tk programmers.

Previous
Radiobutton, Checkbutton 
Next
Menubutton, Menu 
Subscribe to Feed