Tải bản đầy đủ (.pdf) (19 trang)

Web Client Programming with Perl-Chapter 7: Graphical Examples with Perl/Tk- P3

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (39.99 KB, 19 trang )

Chapter 7: Graphical Examples with Perl/Tk- P3

Our destinations list is an almost exact copy of the list you'd see on the web
page. For ease in using, we placed "U.S.A." as the first item in the list, and
we will select it as our default choice when we build the listbox:
my $entry_f = $mw->Frame;
$entry_f->pack(-expand => 'n', -fill => 'x');
$entry_f->Label(-text => "Airbill #: ")->pack(-side
=> 'left',
-anchor => 'w',
-expand => 'n',
-fill => 'none');
my $airbill = "";
my $airbill_entry = $entry_f->Entry(-textvariable
=> \$airbill,
-width => 10);
$airbill_entry->pack(-side => 'left',
-anchor => 'w',
-expand => 'y',
-fill => 'x');
The entry for the airbill requires a label so that the user knows what sort of
input is expected. The default for the $airbill variable is blank. We save a
reference to the entry widget, so that we can set the focus of the application
to it right before we enter the MainLoop :
$entry_f->Label(-text => "Date Shipped: ")->pack(-
side => 'left',
-anchor => 'w',
-expand => 'n',
-fill => 'none');

my %months;



my $i = 1;
foreach (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct
Nov Dec)) {
$months{$_} = $i++;
}

my $fulltime = localtime;

my ($month, $day, $year) = $fulltime =~
/\w+\s(\w+)\s(\d+)\s..:..:..\s..(\d\d)$/;

$month = $months{$month};
$month = "0$month" if (length($month) < 2);
$day = "0$day" if (length($day) < 2);

my $date = "$month$day$year";
$entry_f->Entry(-textvariable => \$date,
-width => 6)->pack(-side => 'left',
-anchor => 'w',
-expand => 'n',
-fill => 'none');
We are going to use a default of today for the date field. The FedEx web
page expects it in the form of "DayMonthYear", and digits with only one
number require a leading zero. The string returned from localtime( ) gives us
the correct day, and we strip off the last two digits of the year. For the month
we need to translate it to a number value from 01 - 12. We do this using a
%months hash, where the keys are the string of the month, and the value the
number of the month. We add leading zeros to the day and month if
necessary.

my $lb_f = $mw->Frame;
$lb_f->pack(-anchor => 'n',
-expand => 'n',
-fill => 'x');
$lb_f->Label(-text => "Shipped To:")->pack(-side =>
'left',
-anchor => 'w');
We want a label to tell us what the listbox contains, so we create it first:
my $scroll = $lb_f->Scrollbar;
my $listbox = $lb_f->Listbox(-selectmode =>
'single',
-height => 1,
-yscrollcommand => ['set',
$scroll],
-exportselection => 0);
$scroll->configure(-command => ['yview',
$listbox]);
$scroll->pack(-side => 'right', -fill => 'y');
$listbox->pack(-side => 'left', -expand => 'yes', -
fill => 'both');

$listbox->insert('end', @destinations);
$listbox->selection('set',0);
Then we create the scrollbar and the listbox, and put our @destinations in
the listbox. Remember, we put the entry "U.S.A" first in our list, so when we
select the 0th element of the listbox, we get that entry selected. This is a
pretty large list, and it takes quite a while to scroll down to Zimbabwe.
Although we didn't do it for our example here, you could set up your listbox
so that if you typed a letter, it would scroll to the first entry starting with that
letter. Or you could put an additional entry, and search for any word starting

with those characters:
my $response_f = $mw->Frame;
$response_f->pack(-expand => 'y', -fill => 'both');

$response_f->Label(-text => "Response:")->pack(-
anchor => 'w',
-side => 'left');

my $response_txt = "";
$response_f->Label(-justify => 'left', -borderwidth
=> 2, -relief => 'sunken',
-textvariable => \$response_txt)-
>pack(-anchor => 'w',
-side =>
'left',
-expand =>
'y',
-fill =>
'x');
To show users what happened to their package (or any errors), we build a
label that displays any text in the $response_txt variable. To change the text,
we simply reset $response_txt to another text string:
my $bttn_f = $mw->Frame;
$bttn_f->pack;

$bttn_f->Button(-text => "Exit", -command =>
sub{exit})
->pack(-side =>'right', -anchor =>
'e');


my $loop_bttn = $bttn_f->Button(-text => "Loop",
-command => \&loop_query);
$loop_bttn->pack(-side => 'left', -anchor => 'w');

$bttn_f->Button(-text => "Query", -command =>
\&do_query)->
pack(-side => 'left',
-anchor => 'w');
The buttons for our track program allow us to exit the program, start the
query loop, or manually do a query right now.
my $pkg_tracker = new FedEx $url, $email;
my $loop_id;

×