Dear Monks,
I need to read the content of a Tk Text Widget every time its content changes. The problem is that focus is always on the text widget and no key is pressed (text is input through speech recognition module with chunks of sentences added every time). At the moment my solution is quite simple: every x ms I read the content of the widget (sampling) and see if something has changed. However, I am not satisfied by this solution. The reason is that a) I guess is computationally not optimized b) it can happen that the sampling breaks the chunkes of texts added into the text widget.
This is what I have now
use warnings; use strict; use Tk; my $mw = MainWindow->new(); my $tx = $mw->Text()->pack(); $mw->repeat(1000, \&readTextWidget); my $CursorPosition="1.0";#starting index in Tk Text MainLoop; sub readTextWidget{ my @Text=$tx->dump($CursorPosition,'end'); my $TextChunk = $Text[1]; $CursorPosition= $Text[5]; if ($text ne "current"){#skip Tk:Text indicators my @words = split / /, $TextChunk ; foreach my $word (@words) { print "$word\n"; } } }
Any better idea to avoid the repeat function and call the sub readTextWidget when a change in the Tk widget occurs?
In reply to read Tk Text Widget when content changes by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |