Problems with Tk Events



Hello,

I am having problems with the Event Object in Perl/Tk 804.025. When I run the example shown on page 365 of Mastering Perl/Tk with the old syntax shown on page 364

#!/usr/bin/perl -w

use Tk;
use strict;

print "Tk version = ", $Tk::VERSION, "\n";
my $mw = MainWindow->new;
my $b = $mw->Button(-text=>'click b1 then b2', -command => \&callback);
$b->bind('<ButtonRelease-2>' => \&callback);
$b->pack;

MainLoop;

sub callback {
my ($widget) = @_;
my $e = $widget->XEvent;
print "\n";
print "widget event = $e\n";
print "\$e->W = ", $e->W, "\n";
}

I get the following warning although, as you can see, $widget was previously defined.

$ perl callbacktest.pl
Tk version = 804.025
Tk::Error: Can't call method "XEvent" on an undefined value at callbacktest.pl line 16.
Tk callback for .button
Tk::__ANON__ at /Library/Perl/5.8.1/darwin-thread-multi-2level/Tk.pm line 246
Tk::Button::butUp at /Library/Perl/5.8.1/darwin-thread-multi-2level/Tk/Button.pm line 111
<ButtonRelease-1>
(command bound to event)
$


Furthermore, when I run it with the new syntax:

#!/usr/bin/perl -w

use Tk ':variables';
use strict;

print "Tk version = ", $Tk::VERSION, "\n";
my $mw = MainWindow->new;
my $b = $mw->Button(-text=>'click b1 then b2', -command => \&callback);
$b->bind('<ButtonRelease-2>' => \&callback);
$b->pack;

MainLoop;

sub callback {
print "\n";
print "\$Tk::event->W = ", $Tk::event->W, "\n";
}

I get the following message:

$ perl callbacktest.pl
Bareword "MainLoop" not allowed while "strict subs" in use at callbacktest.pl line 12.
Execution of callbacktest.pl aborted due to compilation errors.
$


The Perl (v. 5.8.1 on MacOSX 10.3.1) interpreter apparently has problems importing the Tk variables.

Any help with these problems would be much appreciated.

Cheers,

Philippe


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]