Upload
nathaniel-norris
View
226
Download
0
Embed Size (px)
Citation preview
PERL TK
4.Use an IDE
http://spectcl.sourceforge.net/
3 . Use the documentation!
2 .Experiment.
1.Learn the basics
#Simple Window
use Tk;
$mw=MainWindow->new();
MainLoop ;
#Widget Tour use Tk;$mw=MainWindow->new(); $l=$mw->Label(-text => "this is a label"); $l->pack();
$b=$mw->Button(-text => "this is a button"); $b->pack();
$t=$mw->Text(-width => 10,height => 10); $t->insert(end,"this is a text"); $t->pack();
$e=$mw->Entry(width => 0); $e->insert(end,"this is an entry"); $e->pack();
$i=$mw->Listbox(height => 2); $i->insert(end,"listbox element1"); $i->insert(end,"listbox element2"); $i->selectionSet(1); $i->pack();
$be = $mw->BrowseEntry(-label => "Label", -variable => \$var); $be->insert("end", "opt1"); $be->insert("end", "opt2"); $be->insert("end", "opt3"); $be->pack();
MainLoop;
#BUTTONS
use Tk;
$mw=MainWindow->new( -name => "Buttons", -title => 'Button Demonstration', ) ;
$l=$mw->Label(-text => "If you click on any of the four buttons below, the background of the button area will change to the color indicated in the button. You can press Tab to move among the buttons, then press
Space to invoke the current button.", -wraplength => "4i",
- justify => left;)
$l->pack;)(
foreach my $color (qw/PeachPuff1 LightBlue1 SeaGreen2 Yellow1/) {
my $b = $mw->Button( -text => $color, -width => 10, -command =>
sub{$mw->configure(-background => lc($color))} ;) ,
$b->pack(qw/-side top -expand yes -pady 2/);
}
MainLoop ;
#LABELS
use Tk; $mw=MainWindow->new( -name => "Label", -title => 'Label', ); $l=$mw->Label(-text => "Five labels are displayed below: three textual ones on the left, and an image label and a text label on the right. Labels are pretty boring because you can\'t do anything with them.", -wraplength => "4i", -justify => left);
$l->pack();
my @pl = qw/-side left -expand yes -padx 10 -pady 10 -fill both/;
my $left = $mw->Frame->pack(-side => left, -expand => yes, -padx => 10, -pady => 10, -fill => both);
my $right = $mw->Frame->pack(@pl);
@pl = qw/-side top -expand yes -pady 2 -anchor w/;
my $left_l1 = $left->Label(-text => 'First label')->pack(@pl);
my $left_l2 = $left->Label( -text => 'Second label, raised just for fun', -relief => 'raised', )->pack(@pl);
my $left_l3 = $left->Label( -text => 'Third label, sunken', -relief => 'sunken', )-pack(@pl); @pl = qw/-side top/;
my $right_bitmap = $right->Label( -image => $mw->Photo(-file => Tk->findINC('Xcamel.gif')), -borderwidth => 2, -relief => 'sunken', )->pack(@pl);
my $right_caption = $right->Label(-text => 'Perl/Tk')->pack(@pl); MainLoop;
#RADIOBUTTON
use Tk;
sub click{ $mw->Dialog(-title => "Title", -buttons => ["OK"],text => $_[0])->Show(); }
$mw=MainWindow->new( -name => "Radio", -title => 'Radio', );
$v=2;
$b=$mw->Radiobutton(-command =>[ \&click ,["The Radio 1 clicked“,\$v)],-variable => \$v,-value => 1);
$b1=$mw->Radiobutton(-command =>[ \&click("The Radio 2 clicked",\$v1)],-variable => \$v,-value => 2);
$b->pack();
$b1->pack();
MainLoop;
#Try to pack the "Button 10" at the left
use Tk;
$mw=MainWindow->new();
$mw->Button(-text => "Button1")->pack(qw/-side top -pady 2/);
$mw->Button(-text => "Button2")->pack(qw/-side top -pady 2/);
$mw->Button(-text => "Button3")->pack(qw/-side top -pady 2/);
$mw->Button(-text => "Button4")->pack(qw/-side top -pady 2/);
$mw->Button(-text => "Button 10")->pack(qw/-side left -expand yes -pady 2/);
MainLoop;
use Tk;
$mw=MainWindow->new();
$mw->Button(-text => "Button 10")->pack(qw/-side left -pady 2/);
$mw->Button(-text => "Button1")->pack(qw/-side top -pady 2/);
$mw->Button(-text => "Button2")->pack(qw/-side top -pady 2/);
$mw->Button(-text => "Button3")->pack(qw/-side top -pady 2/);
$mw->Button(-text => "Button4")->pack(qw/-side top -pady 2/);
MainLoop;
#Make it to take all the extra space
use Tk;
$mw=MainWindow->new();
$mw->Button(-text => "Button 10", -height => 8 )->pack(qw/-side left -expand yes -pady 2/);
$mw->Button(-text => "Button1")->pack(qw/-side top -expand yes -pady 2/);
$mw->Button(-text => "Button2")->pack(qw/-side top -expand yes -pady 2/);
$mw->Button(-text => "Button3")->pack(qw/-side top -expand yes -pady 2/);
$mw->Button(-text => "Button4")->pack(qw/-side top -expand yes -pady 2/);
MainLoop;
use Tk;
$mw=MainWindow->new();
$mw->Button(-text => "Button 10", -height => 8 )->pack(qw/-side left -expand yes -pady 2 fill y/);
$mw->Button(-text => "Button1")->pack(qw/-side top -expand yes -pady 2/);
$mw->Button(-text => "Button2")->pack(qw/-side top -expand yes -pady 2/);
$mw->Button(-text => "Button3")->pack(qw/-side top -expand yes -pady 2/);
$mw->Button(-text => "Button4")->pack(qw/-side top -expand yes -pady 2/);
MainLoop;
#Make the "Button 1" and "Button 2" to be at the left ,one under another
use Tk; $mw=MainWindow->new();
$mw->Button(-text => "Button 10", -height => 8 )->pack(qw/-side left -expand yes -pady 2 -fill y/);
$mw->Button(-text => "Button 20", -height => 8 )->pack(qw/-side left -expand yes -pady 2 -fill y/);
$mw->Button(-text => "Button1")->pack(qw/-side top -expand yes -pady 2/);
$mw->Button(-text => "Button2")->pack(qw/-side top -expand yes -pady 2/); $mw->Button(-text => "Button3")->pack(qw/-side top -expand yes -pady 2/); $mw->Button(-text => "Button4")->pack(qw/-side top -expand yes -pady 2/); MainLoop;
#Use Frames!
use Tk;
$mw=MainWindow->new();
$f1=$mw->Frame(); $f2=$mw->Frame();
$f1->Button(-text => "Button 10")->pack(qw/-side top -expand yes -pady 2 -fill y/);
$f1->Button(-text => "Button 20")->pack(qw/-side top -expand yes -pady 2 -fill y/);
$f2->Button(-text => "Button1")->pack(qw/-side top -expand yes -pady 2/);
$f2->Button(-text => "Button2")->pack(qw/-side top -expand yes -pady 2/);
$f2->Button(-text => "Button3")->pack(qw/-side top -expand yes -pady 2/);
$f2->Button(-text => "Button4")->pack(qw/-side top -expand yes -pady 2/);
$f1->pack(qw/-side left -expand yes -pady 2 -fill y/); $f2->pack(qw/-side left -expand yes -pady 2 -fill y/);
MainLoop;
#Placer
use Tk;
$mw=MainWindow->new();
$mw->Button(-text => "Button1")->place(qw/-x 1 -y 1/);
$mw->Button(-text => "Button2")->place(qw/-x 1 -y 1/);
MainLoop;
use Tk;
$mw=MainWindow->new();
$mw->Button(-text => "Button1")->place(qw/-x 4 -y 4/);
$mw->Button(-text => "Button2")->place(qw/-x 1 -y 1/);
MainLoop;
That is not all:
Open the real application in the browser…