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

9.21. A Drawing Program Example

The canvas widget is very versatile and can be useful for displaying different types of items. One of the first things that comes to mind when people think of a canvas is a drawing program. To save you the trouble, I've written a rudimentary drawing program called Quick Draw you can use to draw rectangles, ovals, and lines. You can also change the thickness of the objects before you draw them. It only requires a tiny bit of error-checking to make it a slicker program. Here's the code:

use Tk; $mw = MainWindow->new; $mw->title("Quick Draw"); $f = $mw->Frame(-relief => 'groove', -bd => 2, -label => "Draw:")->pack(-side => 'left', -fill => 'y'); $draw_item = "rectangle"; $f->Radiobutton(-variable => \$draw_item, -text => "Rectangle", -value => "rectangle", -command => \&bind_start)->pack(-anchor => 'w'); $f->Radiobutton(-variable => \$draw_item, -text => "Oval", -value => "oval", -command => \&bind_start)->pack(-anchor => 'w'); $f->Radiobutton(-variable => \$draw_item, -text => "Line", -value => "line", -command => \&bind_start)->pack(-anchor => 'w'); $f->Label(-text => "Line Width:")->pack(-anchor => 'w'); $thickness = 1; $f->Entry(-textvariable => \$thickness)->pack(-anchor => 'w'); $c = $mw->Scrolled("Canvas", -cursor => "crosshair")->pack( -side => "left", -fill => 'both', -expand => 1); $canvas = $c->Subwidget("canvas"); &bind_start(); MainLoop; sub bind_start { # If there is a "Motion" binding, we need to allow the user # to finish drawing the ...
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