Focusing on widget 
Author Message
 Focusing on widget

Newbie Perl/Tk user here!

Right now I'm working on trying to get the curser to go to the next text field
on the main window. I would like this to happen on pressing of the 'Tab' key
(And, eventually, also on the Return key). What I have now doesn't seem to work
the way I want it. It instead places a tab within the text field it is currently
in.

A second problem is I can't seem to decipher is to make the text windows so that
it will not take more text then the window has width.

I've looked at the Perl/Tk FAQ and can't seem to find what I'm looking for.

Thanks,
Shain

------ CODE BLOCK -------

#! perl -w

use Tk;

my $main= MainWindow->new;

block('Damage');
block('Range');
block('RoF');
block('Cost');

########################################
# Trying to bind to move to next field #
########################################
$txtDamage->bind("Tab"=> $txtRange->focus );
$txtRange->bind("Tab"=> $txtRoF->focus );
$txtRoF->bind("Tab"=> $txtDamage->focus );

sub block {
        ## This creates a block which includes the frame, label and binding to
        ## update on keypressing.
        ${'frm'.$_[0]} = $main->Frame->pack( '-side'=>'left' );
        ${'lab'.$_[0]} = ${'frm'.$_[0]}->Label( '-text'=>$_[0], '-relief'=>'raised'
                                        )->pack( '-fill'=>'x' );
        ${'txt'.$_[0]} = ${'frm'.$_[0]}->Text( '-width'=>'8', '-height'=>'1',
                                         '-wrap'=>'none')->pack;
        ${'txt'.$_[0]}->bind('<KeyPress>', \&update);

Quote:
}

MainLoop;

sub update {
        my $td=$txtDamage->get('1.0','end')+0;
        my $tr=$txtRange->get('1.0','end')+0;
        my $tb=$txtRoF->get('1.0','end')+0;
        $txtDamage->delete('1.0','end');    
        $txtRange->delete('1.0','end');      
        $txtRoF->delete('1.0','end');
        $txtDamage->insert('end',$td<50?$td:0);  
        $txtRange->insert('end',$tr<20?$tr:0);    
        $txtRoF->insert('end',$tb<10?$tb:0);
        $txtCost->delete('1.0','end');
        $txtCost->insert('end',&compCost);

Quote:
}

sub compCost {
        if (!($txtRoF->get('1.0','end')+0))  
        {
                return $txtDamage->get('1.0','end') **       2
                + $txtRange->get('1.0','end')  ** 3
        } else {
                return $txtDamage->get('1.0','end') ** 2 +
                $txtRange->get('1.0','end') ** 3 +
                ($txtDamage->get('1.0','end')/2 + $txtRoF->get('1.0','end')) ** 2
        };
Quote:
}



Thu, 19 Jun 2003 20:30:08 GMT  
 Focusing on widget

Quote:

> Newbie Perl/Tk user here!

> Right now I'm working on trying to get the curser to go to the next
text field
> on the main window. I would like this to happen on pressing of the
'Tab' key
> (And, eventually, also on the Return key).

Firstly, you should be using an Entry widget rather than Text. You can
use the -validateCommand method to make sure that only numbers are
entered and you can also set a max width by checking the number of
characters on each keypress within each entry. (See manpage for
Tk::Entry)

The Text widget of course inserts the Tabs..that's how most people
indent. If you switch to Tk::Entry (as I strongly suggest), then the
focus is already taken care of for you due to the normal keyboard
traversal. It seems as if you don't want the user to have access to
the 'Cost' entry..so just set the -takefocus option to zero for that
widget (See manpage for Tk::options)

Now, all you have to do is bind the Return to do the same thing.

Stripped code follows (so you get the idea):

###################################
use Tk;
my $main= MainWindow->new;
block('Damage');
block('Range');
block('RoF');
block('Cost');

$txtDamage->bind('<Return>',sub {$txtRange->focus;} );
$txtRange->bind('<Return>',sub {$txtRoF->focus;} );
$txtRoF->bind('<Return>',sub {$txtDamage->focus;} );

sub block {

${'frm'.$_[0]} = $main->Frame->pack( -side =>'left' );
${'lab'.$_[0]} = ${'frm'.$_[0]}->Label(-text=>$_[0], -relief=>'raised'
   )->pack( -fill=>'x' );
${'txt'.$_[0]} = ${'frm'.$_[0]}->Entry( -width=>'8')->pack;
  if ($_[0] eq 'Cost'){
        ${'txt'.$_[0]}->configure(-takefocus=>0);
  }
${'txt'.$_[0]}->bind('<Any-KeyPress>', \&refresh);

Quote:
}

MainLoop;

sub refresh {
 print "You pressed a key in the widget: $_[0]\n";

Quote:
}

#####################################################

Jack

Sent via Deja.com
http://www.deja.com/



Fri, 20 Jun 2003 06:38:06 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. getOpenFile denies focus to widgets

2. Binding to widget focus changes

3. Change default order of focus with composite widgets

4. Scrolled widget and switching the input focus

5. Putting focus in Entry widget in DialogBox....

6. Give a Entry widget focus at program start

7. Q: force keyboard focus on an entry widget?

8. Tab order (focus sequence) of widgets

9. Help With Widget Focus

10. Composite Widget with NO widgets - Possible?

11. Skipping focus on certain widgets with focusNext or Prev?

12. Can Tk Widget and Xt Widget work together?

 

 
Powered by phpBB® Forum Software