Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Hello
I have the following code governing the TAB behavior. TAB should switch the focus between the first two Entry fields. This works fine. What I do not like is that if the Entry 3 gets the focus, and I click on TAB, Tk fires an error (which is expected). How can I "clean" the code and prevent this error? If I am in Entry 3 and press TAB, the focus should simply stay in this Entry field, or go to one of the other two fields (and start moving between the two fields if TAB is hit again). My code:
use strict; use warnings; use Tk; my $mw = MainWindow->new(); my $entry1 = $mw->Entry()->pack(); my $entry2 = $mw->Entry()->pack(); my $entry3 = $mw->Entry()->pack(); my %after = ($entry1 => $entry2, $entry2 => $entry1, ); $mw -> bind('all','<Tab>',sub{ ($after{$_[0]})->focus; } ); $mw->MainLoop(); exit(0);
My error:
Tk::Error: Can't call method "focus" on an undefined value at C:\Users +\FC\Desktop\Entry.pl line 14. <Key-Tab> (command bound to event)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl/Tk binding Tab
by tybalt89 (Monsignor) on Oct 31, 2018 at 14:44 UTC | |
by Anonymous Monk on Oct 31, 2018 at 17:25 UTC | |
by Tux (Canon) on Oct 31, 2018 at 18:05 UTC | |
by zentara (Cardinal) on Oct 31, 2018 at 18:16 UTC |