Click here to Skip to main content
Licence 
First Posted 4 Feb 2004
Views 27,042
Bookmarked 5 times

Simple front end using Perl/Tk

By | 4 Feb 2004 | Article
Simple front end using Perl/Tk.

Introduction

This article shows how to use Perl to write a simple graphical user interface.

Installation

Install the latest version of Perl and Tk from the ActiveState download page.

Using the code

We begin by declaring the MainWindow object and configuring the object properties.

# Create the main window
my $x_win = MainWindow->new();
$x_win->configure(-title=>'Test');
$x_win->geometry('+200+100');

The application is divided into four frames: one on the top (personInfo), one in the middle (midFrame), and two at the bottom for "Load and Save Data" buttons. personInfo is divided into a left frame for labels and right frame for fields.

my $personInfo = $x_win->Frame(-background=>'cyan')->pack(-side=>'top',
                   -fill=>'x');
my $personLeft = $personInfo->Frame(-background=>'cyan')->pack(-side=>'left',
                   -fill=>'x');
my $personRight = $personInfo->Frame(-background=>'cyan')->pack(-side=>'left',
                   -fill=>'x',-padx=>20,-pady=>20);
# Labels
$personLeft->Label(-text=>'Personal Information',-background=>'cyan')->pack();
$personLeft->Label(-text=>'',-background=>'cyan')->pack();
$personLeft->Label(-text=>'Name',-background=>'cyan')->pack();
$personLeft->Label(-text=>'Age',-background=>'cyan')->pack();
$personLeft->Label(-text=>'Address',-background=>'cyan')->pack();
$personLeft->Label(-text=>'Occupation',-background=>'cyan')->pack();

Variables are bound to the labels in the midFrame frame. This implies that when the state of the label control changes, the variable is updated to reflect the new state and vice-versa.

# Fields
$personRight->Label(-text=>'',-background=>'cyan')->pack();
$personRight->Label(-text=>'',-background=>'cyan')->pack();
$personRight->Entry(-background=>'green',-width=>20,-borderwidth=>2,
    -relief=>'sunken',-textvariable=>\$perInfo->{NAME})->pack();
$personRight->Entry(-background=>'green',-width=>20,-borderwidth=>2,
    -relief=>'sunken',-textvariable=>\$perInfo->{AGE})->pack();
$personRight->Entry(-background=>'green',-width=>20,-borderwidth=>2,
    -relief=>'sunken',-textvariable=>\$perInfo->{ADDR})->pack();
$personRight->Entry(-background=>'green',-width=>20,-borderwidth=>2,
    -relief=>'sunken',-textvariable=>\$perInfo->{OCCUPATION})->pack();

midFrame is used to put some sort of separator between the "Personal Information" and the "Load and Save Data" buttons.

# Separator Bar
my $midFrame = $x_win->Frame(-borderwidth=>3,-relief=>'groove',
                             -background=>'purple',
)->pack(-side=>'top');
$midFrame->Label(width=>100,height=>0,-foreground=>'white',
-background=>'purple')->pack();

# Process frame
my $saveFrame = $x_win->Frame(-background=>'cyan')->pack(-side=>'top',
                              -fill=>'x');
$saveFrame->Button(-text => 'Save Data',-command => \&saveinfo,
              )->grid(qw/-row 2 -column 0 -sticky nesw/);

# Process frame
my $loadFrame = $x_win->Frame(-background=>'cyan')->pack(-side=>'top',
                              -fill=>'x');
$loadFrame->Button(-text => 'Load Data',-command => \&loadinfo,
              )->grid(qw/-row 2 -column 0 -sticky nesw/);

MainLoop() signals the end of TK widgets building, and the code that follows will be subroutines. saveInfo dumps the state of perInfo hash, loadInfo does the opposite. Note that perInfo is bound to the controls in the personRight frame.

MainLoop();

sub saveinfo
{
    open(OUT,">test.txt");
    print OUT "Name" . "=" . $perInfo->{NAME} . "<br>\n";
    print OUT "Age" .  "=" . $perInfo->{AGE} . "<br>\n";
    print OUT "Address" .  "=" . $perInfo->{ADDR} . "<br>\n";
    print OUT "Occupation:" . "=" . $perInfo->{OCCUPATION} . "<br>\n";
    close(OUT);

    $file = $x_win->getSaveFile(-filetypes => \@types);
    open(OUT,">$file");
    #print OUT "Content-type:text/html\n\n";
    print OUT $perInfo->{NAME} . "\n";
    print OUT $perInfo->{AGE} . "\n";
    print OUT $perInfo->{ADDR} . "\n";
    print OUT $perInfo->{OCCUPATION} . "\n";
    close(OUT);
}

sub loadinfo
{
    my $file;
    
    # Types are listed in the dialog widget
    my @types = (["Config Files", '.txt', 'TEXT'],
               ["All Files", "*"] );

    $file = $x_win->getOpenFile(-filetypes => \@types);

    open IN,$file;
    my @tfile = <IN>;
    chop @tfile;
    close (IN);

    $perInfo->{NAME} = $tfile[0];
    $perInfo->{AGE} = $tfile[1];
    $perInfo->{ADDR} = $tfile[2];
    $perInfo->{OCCUPATION} = $tfile[3];
}

This article introduced the basics of installing the Perl interpreter and writing a visual application using the Tk. I encourage you to explore further on the web as well as try out simple programs.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

PrashanthLA

Web Developer

India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
AnswerMissing save button PinmemberMember 474979115:13 17 Sep '09  
GeneralErrors in the code Pinmembermattrix0074:43 26 Apr '06  
GeneralWhere's the source PinmemberGisle Vanem21:40 4 Feb '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 5 Feb 2004
Article Copyright 2004 by PrashanthLA
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid