#!/usr/bin/perl use strict; use warnings; use Tk; my @lines; my $mw = MainWindow->new; my $c = $mw->Canvas(-width => 900, -height => 900,)->pack(-side => 'bottom'); $mw->Button(-text => $_->[0], -command => $_->[1])->pack(-side => 'right') for [ Exit => sub { $mw->destroy } ], [ 'Clear Last' => sub { pop @lines; redraw() } ], [ 'Clear All' => sub { @lines = (); redraw() } ]; $c->Tk::bind('<1>' => sub { push @lines, [ &xy ]; redraw(); $c->Tk::bind('' => sub { push @{ $lines[-1] }, &xy; redraw() } ); } ); $c->Tk::bind('' => sub { $c->Tk::bind('' => '') } ); MainLoop; sub xy { $_[0]->XEvent->x, $_[0]->XEvent->y } sub redraw { $c->delete('pen'); @$_ >= 4 and $c->createLine(@$_, -smooth => 1, -width => 5, -tag => 'pen') for @lines; }