Skip to Main Content
Learning Perl/Tk
book

Learning Perl/Tk

by Nancy Walsh
January 1999
Beginner content levelBeginner
373 pages
9h 43m
English
O'Reilly Media, Inc.
Content preview from Learning Perl/Tk

8.3. A Short Break for a Simple Example

Before we get into some of the more complex (and more fun) things you can do with a text widget, let's look at complete use of the text widget.

This is a short program that will display a file, let you make changes to it, and then save it:

use Tk; $mw = MainWindow->new; # Create necessary widgets $f = $mw->Frame->pack(-side => 'top', -fill => 'x'); $f->Label(-text => "Filename:")->pack(-side => 'left', -anchor => 'w'); $f->Entry(-textvariable => \$filename)->pack(-side => 'left', -anchor => 'w', -fill => 'x', -expand => 1); $f->Button(-text => "Exit", -command => sub { exit; } )-> pack(-side => 'right'); $f->Button(-text => "Save", -command => \&save_file)-> pack(-side => 'right', -anchor => 'e'); $f->Button(-text => "Load", -command => \&load_file)-> pack(-side => 'right', -anchor => 'e'); $mw->Label(-textvariable => \$info, -relief => 'ridge')-> pack(-side => 'bottom', -fill => 'x'); $t = $mw->Scrolled("Text")->pack(-side => 'bottom', -fill => 'both', -expand => 1); MainLoop; # load_file checks to see what the filename is and loads it if possible sub load_file { $info = "Loading file '$filename'..."; $t->delete("1.0", "end"); if (!open(FH, "$filename")) { $t->insert("end", "ERROR: Could not open $filename\n"); return; } while (<FH>) { $t->insert("end", $_); } close (FH); $info = "File '$filename' loaded"; } # save_file saves the file using the filename in the entry box. sub save_file { $info = "Saving '$filename'"; open (FH, ">$filename"); ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Perl/Tk

Mastering Perl/Tk

Stephen Lidie, Nancy Walsh

Publisher Resources

ISBN: 1565923146Supplemental ContentCatalog PageErrata