Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Making a derived Tk::Text object

by zentara (Archbishop)
on Feb 03, 2005 at 20:12 UTC ( [id://427797]=perlquestion: print w/replies, xml ) Need Help??

zentara has asked for the wisdom of the Perl Monks concerning the following question:

Hi, an interesting question has come up on comp.lang.perl.tk, which got me thinking on how to generally solve an "object modification" problem.

The original problem was how to prevent a Tk::Text widget from printing control characters. For instance in this code:

#!/usr/bin/perl use warnings; use strict; #use lib '.'; #BEGIN { unshift(@INC,'.')} use Tk; use Tk::Text; my $top = MainWindow->new(); my $text = $top->Text->pack; $text->bind('<Control-g>', sub {$text->insert('end',"<-How do I supress this?\n") }); MainLoop;
So I looked into the Tk::Text module and right at the top, there is a sub bindRdOnly{}, to which if I add the line
sub bindRdOnly{ $mw->bind($class,'<Control-KeyPress>','NoOp'); .... ..... }
It modifies the Text widget to ignore printing Control chars. I know it works, because I copied Text.pm to the '.' directory, modified it, and ran it as Tk::Text. That is a clunky solution, which I'm embarrased to tell people is a solution, especially since Tk::Text has an xs component and can't just be renamed.

So my understanding of deriving one module from another is not the best. So how do I do one thing....add that binding to make a new package. A similar Tk::Text widget can be made like this: (code from Marc Dashevsky)

package Tk::MyText; require Tk::Text; use base qw/Tk::Text/; Construct Tk::Widget 'MyText'; sub InsertKeypress { my($w, $char) = @_; if (ord($char) < 27) { print chr(ord($char) | 0x40), "\n"; # only Control-G/J/L/M/Q/R/S/U/W/Y/Z are making it to here } else { $w->SUPER::InsertKeypress($char); } }
This one filters the Control keypresses thru the InsertKeypress sub, but it would be nicer to do it through the bindRdOnly sub. Anyone know how to make this simple addition to a module's subroutine to make a different derived package? It is not as simple as just pushing the statement into an array or hash in the package, the sub bindRdOnly{} is just a series of bind statements. Thanks

I'm not really a human, but I play one on earth. flash japh

Replies are listed 'Best First'.
Re: Making a derived Tk::Text object
by zentara (Archbishop) on Feb 03, 2005 at 20:36 UTC
    I did come up with this, but I'm not sure if it is the best way, since the bind is not in the sub bindRdOnly{}, and may have some unforseen impact on Control Bindings.
    #!/usr/bin/perl use warnings; use strict; use Tk; package Tk::MyText; require Tk::Text; use base qw/Tk::Text/; Construct Tk::Widget 'MyText'; sub ClassInit { my($class,$mw) = @_; $class->SUPER::ClassInit($mw); $mw->bind($class,'<Control-KeyPress>','NoOp'); $class; } ##################################################### package main; my $top = tkinit; my $text = $top->MyText->pack; $text->bind('<Control-g>', sub {$text->insert('end',"<-How do I supress this?\n") }); MainLoop;

    I'm not really a human, but I play one on earth. flash japh

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://427797]
Approved by holli
Front-paged by Courage
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-03-29 12:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found