For Programmers: Free Programming Magazines  


Home > Archive > PerlTk > July 2007 > Text bind command not selecting text









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Text bind command not selecting text
sajohn

2007-07-09, 10:02 pm

I am putting together a small report editor and need some help with
the following problem. I would like the user to type in some text and
then later be able to select either all of it or a portion of it for
formatting (ie. centering, bold, italic,etc.) by typing in a command
like Ctrl-A. The following code is what I came up for this process but
for some reason the text does not get selected. Can someone tell why
this code doesn't work?


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

use strict ;
use Tk ;
use Tk::NoteBook ;


my $fTab ;
my $mw = MainWindow->new;
$fTab = $mw->Frame()->pack( -anchor => 'n', -fill => 'both', -expand
=> 1 ) ;

my $nb = $fTab->NoteBook()->pack( -anchor => 'n', -fill=>'both', -
expand=>1 );
my $rpt = $nb->add("Report", -label => "Report");
my $hdr = $nb->add("Header", -label => "Header" );
my $tle = $nb->add("Title", -label => "Title");
my $dtl = $nb->add("Detail", -label => "Detail");
my $ftr = $nb->add("Footer", -label => "Footer");
my $fin = $nb->add("Final", -label => "Final");

my $rpt_txt = $rpt->Text( -width => 80
, -height => 20
, -state => 'disable'
, -relief => 'flat'
)->pack( -anchor => 'n' ) ;

my $hdr_txt = $hdr->Text( -width => 80
, -height => 6
)->pack( -anchor => 'n' ) ;
$hdr_txt->tagConfigure( 'center', justify => 'center', -foreground =>
'red' ) ;
$hdr_txt->bind( '<Control-Key-a>', sub { print "In sub\n" ;
my $t = shift ;
$t->tagAdd( 'center',
'sel.first', 'sel.last') if( $t->tagRanges( 'sel' ) ) ;
} ) ;

my $tle_txt = $tle->Text( -width => 80, -height => 6 )->pack( -anchor
=> 'n' ) ;
my $dtl_txt = $dtl->Text( -width => 80, -height => 6 )->pack( -anchor
=> 'n' ) ;
my $ftr_txt = $ftr->Text( -width => 80, -height => 6 )->pack( -anchor
=> 'n' ) ;
my $fin_txt = $fin->Text( -width => 80, -height => 6 )->pack( -anchor
=> 'n' ) ;



MainLoop;

Jack

2007-07-10, 10:02 pm


"sajohn" <sajohn52@yahoo.com> wrote in message
news:1184028392.176358.236900@q75g2000hsh.googlegroups.com...
>I am putting together a small report editor and need some help with
> the following problem. I would like the user to type in some text and
> then later be able to select either all of it or a portion of it for
> formatting (ie. centering, bold, italic,etc.) by typing in a command
> like Ctrl-A. The following code is what I came up for this process but
> for some reason the text does not get selected. Can someone tell why
> this code doesn't work?
>
>
> ########################################
########################
> #
>
> use strict ;
> use Tk ;
> use Tk::NoteBook ;
>
>
> my $fTab ;
> my $mw = MainWindow->new;
> $fTab = $mw->Frame()->pack( -anchor => 'n', -fill => 'both', -expand
> => 1 ) ;
>
> my $nb = $fTab->NoteBook()->pack( -anchor => 'n', -fill=>'both', -
> expand=>1 );
> my $rpt = $nb->add("Report", -label => "Report");
> my $hdr = $nb->add("Header", -label => "Header" );
> my $tle = $nb->add("Title", -label => "Title");
> my $dtl = $nb->add("Detail", -label => "Detail");
> my $ftr = $nb->add("Footer", -label => "Footer");
> my $fin = $nb->add("Final", -label => "Final");
>
> my $rpt_txt = $rpt->Text( -width => 80
> , -height => 20
> , -state => 'disable'
> , -relief => 'flat'
> )->pack( -anchor => 'n' ) ;
>
> my $hdr_txt = $hdr->Text( -width => 80
> , -height => 6
> )->pack( -anchor => 'n' ) ;
> $hdr_txt->tagConfigure( 'center', justify => 'center', -foreground =>
> 'red' ) ;
> $hdr_txt->bind( '<Control-Key-a>', sub { print "In sub\n" ;
> my $t = shift ;
> $t->tagAdd( 'center',


#Make sure you bind to a cApitAl A too...
$hdr_txt->bind( '<Control-Key-a>', [$hdr_txt => 'selectAll']);
$hdr_txt->bind( '<Control-Key-A>', [$hdr_txt => 'selectAll']);



> 'sel.first', 'sel.last') if( $t->tagRanges( 'sel' ) ) ;
> } ) ;
>
> my $tle_txt = $tle->Text( -width => 80, -height => 6 )->pack( -anchor
> => 'n' ) ;
> my $dtl_txt = $dtl->Text( -width => 80, -height => 6 )->pack( -anchor
> => 'n' ) ;
> my $ftr_txt = $ftr->Text( -width => 80, -height => 6 )->pack( -anchor
> => 'n' ) ;
> my $fin_txt = $fin->Text( -width => 80, -height => 6 )->pack( -anchor
> => 'n' ) ;
>
>
>
> MainLoop;
>


Jack


Ch Lamprecht

2007-07-12, 10:03 pm

sajohn wrote:
> I am putting together a small report editor and need some help with
> the following problem. I would like the user to type in some text and
> then later be able to select either all of it or a portion of it for
> formatting (ie. centering, bold, italic,etc.) by typing in a command
> like Ctrl-A. The following code is what I came up for this process but
> for some reason the text does not get selected. Can someone tell why
> this code doesn't work?
>


Hello,

the Ctrl-A event is processed by a Class binding first. Therefore your 'sel' tag
does no longer exist at the time your instance-binding is invoked.
So you have to bind to the Text class, change the bindtags order or derive a
Class from Tk::Text which provides the behaviour that you need.

The first approach is the simplest.
The drawback here is, that all Text widgets in your application will respond to
the binding:

use warnings;
use strict;
use Tk;



my $fTab ;
my $mw = MainWindow->new;
$fTab = $mw->Frame()->pack( -anchor => 'n',
-fill => 'both',
-expand => 1 ) ;



my $hdr_txt = $fTab->Text( -width => 80,
-height => 6
)->pack( -anchor => 'n' ) ;
$hdr_txt->tagConfigure( 'center',
-justify => 'center',
-foreground => 'red' ) ;
$mw->bind( 'Tk::Text','<Control-Key-a>',\&event_);

MainLoop;

sub event_{
my $t = shift ;
$t->tagAdd( 'center',
'sel.first',
'sel.last') if( $t->tagRanges( 'sel' ) ) ;
}


--
use Tk;use Tk::GraphItems;$c=tkinit->Canvas->pack;push@i,Tk::GraphItems->
TextBox(text=>$_,canvas=>$c,x=>$x+=70,y=>100)for(Just=>another=>Perl=>Hacker);
Tk::GraphItems->Connector(source=>$i[$_],target=>$i[$_+1])for(0..2);
$c->repeat(30,sub{$_->move(0,4*cos($d+=3.16))for(@i)});MainLoop
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com