I am trying to write simple application, which allows autocompletion while typing text. There is very good widget Tk::MatchEntry, but allows only autocompletion of whole string. I am aiming at autocompletion on every word I type. Below is what I did to this moment: app, widgets, binding. The problem is with selection management in the OnKeyPress() function. If I could extract properly whole content from textbox and extract selection text I would handle the rest.

I've read Tk manual, am aware of 'sel.first', 'sel.last'. It seems to me I won't have to translate from 2D to 1D format if I use 'delete', 'insert' (and other Tk::Entry commands) in some clever way.

In the comment at the end of the code is the idea of what I want to achieve. Any help/comment is welcome.

#!/usr/local/bin/perl use Tk; use Tk ':variables'; use strict; use warnings; # Main Window my $mw = new MainWindow; #GUI Building Area my $frm_name = $mw -> Frame(); #Text Area my $textarea = $mw -> Frame(); my $txt = $textarea -> Text(-width=>40, -height=>10); my $srl_y = $textarea -> Scrollbar(-orient=>'v',-command=>[yview => $t +xt]); my $srl_x = $textarea -> Scrollbar(-orient=>'h',-command=>[xview => $t +xt]); $txt -> configure(-yscrollcommand=>['set', $srl_y], -xscrollcommand=>[ +'set',$srl_x]); my $log = $textarea -> Text(-width=>40, -height=>2); #Geometry Management $frm_name -> grid(-row=>1,-column=>1,-columnspan=>2); $txt -> grid(-row=>1,-column=>1); $log -> grid(-row=>6,-column=>1); $srl_y -> grid(-row=>1,-column=>2,-sticky=>"ns"); $srl_x -> grid(-row=>2,-column=>1,-sticky=>"ew"); $textarea -> grid(-row=>5,-column=>1,-columnspan=>2); $txt->bind('<KeyPress>', [\&OnKeyPress, $txt]); my @dictionary = qw/rewelation reduction renowation reckonesance reval +uation reply/; $txt->focus; MainLoop; sub OnKeyPress { my $txtBox = $_[0]; # print 'Keysym=', $Tk::event->K, ', numeric=', $Tk::event->N, "\n +"; my @r = $txtBox->tagRanges('sel'); if(@r) { my $str = $txtBox->get(); my $sel = substr $str, $r[0], $r[1]-$r[0]; print 'selected: ', $sel; } } =the rest sub OnKeyPressed { if(key is <ENTER>) { accept_auto_complete_version() } elsif(matches_first_char_in_autocompletion_version()) { unselect this char; } else { unselect this char; find_other_autocompletion; } } =cut

In reply to Tk::Entry selection problem by grizzley

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.