O'Reilly logo

Mastering Perl/Tk by Nancy Walsh, Stephen Lidie

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Waiting for Events to Happen

At certain points in your application, it makes sense to wait until something happens. For instance, if you create a ColorEditor window and want it to assign the color the user selects to a variable, you can use waitVariable to wait until the variable is set. For complete details, see Chapter 15.

To have a program wait until a variable’s value is changed, call waitVariable:

$widget->waitVariable(\$var);

Processing will continue as soon as the value contained within $var is changed to something different. To wait until a $widget is visible, use waitVisibility:

$widget->waitVisibility;

To wait until a widget is destroyed, call waitWindow:

$widget->waitWindow;

When you call these methods, nothing will happen in your program until the requested event has taken place.

An alternative to waitWindow is OnDestroy, where you specify a callback. The widget methods are still available when you use OnDestroy:

$widget->OnDestroy(sub { ... });

File Events

There is a special method in Perl/Tk called fileevent, which watches and notifies you when a file is readable or writable. For complete details see Chapter 15, Chapter 19, and Chapter 22.

Here is an example that shows how fileevent can be used (this code is meant to be executed on a Unix system because we use the Unix tail command):[1]

use Tk; open (FH, "tail -f -n 25 text_file|") || die "Could not open file!\n"; my $mw = MainWindow->new; my $text = $mw->Scrolled("Text", -width => 80, -height => 25)->pack(-expand ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required