28
PERL TK

PERL TK. 4.Use an IDE 3. Use the documentation! 2. Experiment. 1.Learn the basics

Embed Size (px)

Citation preview

Page 1: PERL TK. 4.Use an IDE  3. Use the documentation! 2. Experiment. 1.Learn the basics

PERL TK

Page 2: PERL TK. 4.Use an IDE  3. Use the documentation! 2. Experiment. 1.Learn the basics

4.Use an IDE

http://spectcl.sourceforge.net/

3 . Use the documentation!

2 .Experiment.

1.Learn the basics

Page 3: PERL TK. 4.Use an IDE  3. Use the documentation! 2. Experiment. 1.Learn the basics

#Simple Window

use Tk;

$mw=MainWindow->new();

MainLoop ;

Page 4: PERL TK. 4.Use an IDE  3. Use the documentation! 2. Experiment. 1.Learn the basics

#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;

Page 5: PERL TK. 4.Use an IDE  3. Use the documentation! 2. Experiment. 1.Learn the basics
Page 6: PERL TK. 4.Use an IDE  3. Use the documentation! 2. Experiment. 1.Learn the basics

#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 ;

Page 7: PERL TK. 4.Use an IDE  3. Use the documentation! 2. Experiment. 1.Learn the basics
Page 8: PERL TK. 4.Use an IDE  3. Use the documentation! 2. Experiment. 1.Learn the basics

#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;

Page 9: PERL TK. 4.Use an IDE  3. Use the documentation! 2. Experiment. 1.Learn the basics
Page 10: PERL TK. 4.Use an IDE  3. Use the documentation! 2. Experiment. 1.Learn the basics

#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;

Page 11: PERL TK. 4.Use an IDE  3. Use the documentation! 2. Experiment. 1.Learn the basics
Page 12: PERL TK. 4.Use an IDE  3. Use the documentation! 2. Experiment. 1.Learn the basics

#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;

Page 13: PERL TK. 4.Use an IDE  3. Use the documentation! 2. Experiment. 1.Learn the basics
Page 14: PERL TK. 4.Use an IDE  3. Use the documentation! 2. Experiment. 1.Learn the basics

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;

Page 15: PERL TK. 4.Use an IDE  3. Use the documentation! 2. Experiment. 1.Learn the basics
Page 16: PERL TK. 4.Use an IDE  3. Use the documentation! 2. Experiment. 1.Learn the basics

#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;

Page 17: PERL TK. 4.Use an IDE  3. Use the documentation! 2. Experiment. 1.Learn the basics
Page 18: PERL TK. 4.Use an IDE  3. Use the documentation! 2. Experiment. 1.Learn the basics

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;

Page 19: PERL TK. 4.Use an IDE  3. Use the documentation! 2. Experiment. 1.Learn the basics
Page 20: PERL TK. 4.Use an IDE  3. Use the documentation! 2. Experiment. 1.Learn the basics

#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;

Page 21: PERL TK. 4.Use an IDE  3. Use the documentation! 2. Experiment. 1.Learn the basics
Page 22: PERL TK. 4.Use an IDE  3. Use the documentation! 2. Experiment. 1.Learn the basics

#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;

Page 23: PERL TK. 4.Use an IDE  3. Use the documentation! 2. Experiment. 1.Learn the basics
Page 24: PERL TK. 4.Use an IDE  3. Use the documentation! 2. Experiment. 1.Learn the basics

#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;

Page 25: PERL TK. 4.Use an IDE  3. Use the documentation! 2. Experiment. 1.Learn the basics
Page 26: PERL TK. 4.Use an IDE  3. Use the documentation! 2. Experiment. 1.Learn the basics

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;

Page 27: PERL TK. 4.Use an IDE  3. Use the documentation! 2. Experiment. 1.Learn the basics
Page 28: PERL TK. 4.Use an IDE  3. Use the documentation! 2. Experiment. 1.Learn the basics

That is not all:

Open the real application in the browser…