#!/usr/bin/perl use strict; use Tk; my $after_id; my $mw = MainWindow->new(); $mw->Label(-text => "Type a phrase in the entry box below")->pack(); $mw->Label(-text => "Then click once to print all of it")->pack(); $mw->Label(-text => "Or double-click to print just one word")->pack(); my $ent = $mw->Entry(-width => 25)->pack(); $ent->bind( "", [\&print_me, "word"] ); $ent->bind( "", sub { $after_id = $ent->after( 500, [\&print_me, $ent] ) } ); MainLoop; sub print_me { my ( $widget, $mode ) = @_; my $content = $widget->get(); if ( $mode eq "word" ) { $widget->afterCancel( $after_id ); my $start = $widget->index( 'sel.first' ); my $length = $widget->index( 'sel.last' ) - $start; print "you double-clicked the word: " . substr( $content, $start, $length), $/; } else { print $content,$/; } }