Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: cursor position in text widget and linenumbertext

by liverpole (Monsignor)
on Sep 15, 2009 at 14:22 UTC ( [id://795395]=note: print w/replies, xml ) Need Help??


in reply to cursor position in text widget and linenumbertext

Hi balee,

I don't see in the documentation for Tk::LineNumberText anything about the method index() being available the way it is for Tk::Text.

Having said that, though, it appears (to address your second question first) that you could access the subwidget like this:

use strict; use warnings; use Tk; use Tk::LineNumberText; my $mw = MainWindow->new; my $t = $mw->LineNumberText('Text')->pack(); $t->insert('end', "This is some\n"); $t->insert('end', "normal text\n"); $t->bind('<Button-1>' => [\&updateStatus]); $mw->bind('<question>' => [\&updateStatus]); # Debug section -- shows objects' keys use Data::Dumper; my @tkeys = keys %$t; my $sw = $t->{'SubWidget'}; my @swkeys = keys %$sw; printf "LineNumberText keys .... %s\n", Dumper([@tkeys]); printf "Subwidget keys ......... %s\n", Dumper([@swkeys]); MainLoop(); sub updateStatus { my $text = $sw->{'text'}; # Get Text obj (Not really a good p +ractice!) my $status = $text->index('current'); print "$status\n"; }

Note, however, that the line my $text = $sw->{'text'}; breaks the OO encapsulation; you're not really supposed to be accessing the data from the Tk::LineNumberText object directly (and the interface could change in the future, leaving you out of luck!)

Maybe you could suggest to the author of Tk::LineNumberText to put in a method to access the underlying Text object.

For your first question, I'm not exactly sure what you're asking.  The line $mw->bind('<question>' => [\&updateStatus]); shows how you could bind the question mark "?" to the subroutine, but it sounds like you're asking more about the cursor keys in particular.  Can you be a bit more specific about what you're trying to do?


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

Replies are listed 'Best First'.
Re^2: cursor position in text widget and linenumbertext
by balee (Novice) on Sep 15, 2009 at 20:50 UTC

    Thanks for your help!

    Now I can see what was the problem with Linenumbertext.

    My first problem was that statusupdate happened only in case of clicking in the text with the mousebutton. I also wanted to update status every keystroke... Meanwhile - with a little help - I figured out: $mw->bind('<KeyPress>' => [\&updateStatus]); In case of linenumbertext, it works, but with simple Text, - I don't understand why - it doesn't.
    use Tk; use Tk::LineNumberText; $mw = MainWindow->new; $t = $mw->Text()->pack(); #$t = $mw->LineNumberText('Text')->pack(); $t->insert('end', "This is some\n"); $t->insert('end', "normal texaaaat\n"); $t->insert('end', "normal textasdfasdf\n"); $t->insert('end', "normal textasdasd\n"); $t->bind('<Button-1>' => [\&updateStatus]); $t->bind('<KeyPress>' => [\&updateStatus]); MainLoop(); sub updateStatus { #my ($line,$col) = split(/\./,$t->{'rtext'}->index('insert')); #$status = "line: $line, col: $col"; $status = $t->index('current'); print "$status\n"; }
      Hi,

      seems to work with 'insert' :

       perl -MTk -e'$t = tkinit->Text->pack;$t->bind("<KeyPress>", sub{print $t->index("insert"),"\n"});MainLoop'
      Cheers, Christoph

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://795395]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (None)
    As of 2024-04-25 01:33 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found