Re: Problems with Tk Events



Philippe de Rochambeau schrieb:
Hello,

You are wrong here. This is the GTK List, not the Tk list. Try the usenet group comp.lang.perl.tk. There are _very_ helpful people - like here, but they know about Tk :-)


But if you are alreasdy here. I'll give it a try. My Tk-Experience is some time ago...


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

Courier0000,0000,FFFE#!/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.

Courier0000,0000,FFFE$ perl callbacktest.pl
Tk version = 804.025
Tk::Error: Can't call method "XEvent" on an undefined value at callbacktest.pl line 16.

Yes - you defined $widget as a "my" variable. But this is not the message. The message is: $window contains the "undef" value. This means that your subroutine was called without parameter.

How can this be? Where is it called? It is called via the callback mechanism. I don't really know wether this mechanism automatically adds the widget value but if you want to be shure you should read

  perldoc Tk::callbacks

I myself use mostly the closure syntax for callbacks. With this syntax I have a better feeling about what happens inside the callback and which arguments are used (but its personal taste):

  my $b = $mw->Button(
    -text=>'click b1 then b2',
    -command => sub{callback($my,$own,$parameters,$first,$then,@_)},
  );



The example you used was made for keypress events, not for button-1 events.

In the book "Mastering Perl/Tk" on page 365 is explained that the callbacks doesn't get arguments if you click button 1. No arguments = no value = undef value = error message!



Furthermore, when I run it with the new syntax:

Courier0000,0000,FFFE#!/usr/bin/perl -w

use Tk ':variables';
> [...]
MainLoop;
> [...]
Bareword "MainLoop" not allowed while "strict subs" in use at callbacktest.pl line 12.
Execution of callbacktest.pl aborted due to compilation errors.

If you only export ":variables", the MainLoop subroutine will not be exported. This has nothing to do with the above problem. Perhaps this helps:

use Tk;
use Tk ':variables';


Any help with these problems would be much appreciated.

Cheers,

Philippe

Thomas




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