Monday 3 February 2014

Inserting values in Perl Tk Listbox

Perl programming language can be used to design GUI interfaces as well. Some most known modules used for this are Tk, Prima, Gtk2, wxPerl. Most popular and broadly used module is the Tk module. Thanks to the extensive documentation and easy learning curve.

In this post, we'll cover a basic tutorial using Perl Tk. We will create a basic listbox and insert array values into it.

First of all, download the Tk module and install it. If you're not aware about how to install Perl modules, we'll cover those basics in another post.

Consider the following piece of code :

use Tk;
use Tk::Listbox;

#This is the array containing values, which will be inserted into the listbox

my @values1 = qw(China Russia India Brazil USA UK);

#This is the code for defining the main window size along with the window title.

my $m = MainWindow->new;
$m->geometry("320x300");

$m->title("Listbox values Example");

# This is the definition of the Listbox. Try tweaking its parameters to modify its design.

my $lb = $m->Listbox(
-background =>'Yellow',
-font => 'Helvetica',
-foreground => "Red",
-selectborderwidth => '2',
-selectmode => "single",
-width => "15",
-exportselection => 1
)->pack();

#This is the syntax for inserting values into the defined listbox.

$lb->insert('end', @values1);

MainLoop;

Save the program as ListBox_Eg.pl and run it. (perl ListBox_Eg.pl). The output window would be as follows :


Note the above code will work on Windows and OSX alike, since Perl is a platform independent programming language.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker