in reply to changing the focusing-order in Tk

You should be able to pass the reference to the bind command without the anon sub. It should look something like this:
&defineOrder($widget1, $widget2, $widget3) # sub defineOrder { for (my $i = 0; defined($_[$i+1]); $i++) { # REMOVE the anonymous sub call and replace with # reference to widget which has its own scope ${$_[$i]}->bind('<Return>', \${$_[$i+1]}->focus ); } ${$_[0]}->focus; }

this should make the widget scoping work better. This isn't tested so YMMV.

--
hiseldl
"Act better than you feel"

Replies are listed 'Best First'.
Re: Re: changing the focusing-order in Tk
by schweini (Friar) on Sep 26, 2002 at 04:27 UTC
    thanks a lot! (++)
Re: Re: changing the focusing-order in Tk
by schweini (Friar) on Sep 27, 2002 at 23:31 UTC
    darn.
    i was mistaken - it DOESN'T work that way!
    (i was a bit confused, and forgot to actually call that sub, and still had some hardcoded define-the-focus-order code flying around in a dark corner of my script, so i didn't even notice that it didn't.
    help, anyone?
    please?

    -schweini
      okay, here's a script illustrating the problem:
      use Tk; use strict; use warnings; my $win = MainWindow->new(); $win->Button(-text => 'Other Window', -command => \&otherwindow)->pack +; sub otherwindow { my $otherwin = $win->Toplevel; my $foo = $otherwin->Entry->pack; my $bar = $otherwin->Entry->pack; my $baz = $otherwin->Entry->pack; &defineOrder(\$foo, \$bar, \$baz); } sub defineOrder { for (my $i = 0; defined($_[$i+1]); $i++) { ${$_[$i]}->bind('<Return>' , \${$_[$i+1]}->focus ); print ${$_[$i]}, "\n"; } ${$_[0]}->focus; # BTW: $foo->focus() wont work here.... } MainLoop();

      i'm starting to think this is a scoping problem - but then aain, this script doesn't throw ANY warnings.
      dazzled,

      -schweini
        Try this:

        use Tk; use strict; use warnings; my $win = MainWindow->new(); $win->Button(-text=>'Other Window',-command=>\&otherwindow)->pack; sub otherwindow { my $otherwin = $win->Toplevel; my $foo = $otherwin->Entry->pack; my $bar = $otherwin->Entry->pack; my $baz = $otherwin->Entry->pack; &defineOrder($foo, $bar, $baz); } sub defineOrder { my $widget; for (my $i=0; defined( $_[$i+1] ); $i++) { $_[$i]->bind('<Key-Return>', [\&focus, $_[$i+1]]); } # Uncomment this line if you want to wrap around #$_[$#_]->bind('<Key-Return>', [\&focus, $_[0]]); $_[0]->focus; } sub focus { my ($tk, $self) = @_; $self->focus; } MainLoop();

        This changes focus to the next widget using the return key. The focus does not wrap around, but you should be able to add an extra bind command to go from the last widget to the first widget using the example above.

        --
        hiseldl
        What time is it? It's Camel Time!