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

Hi, is there any event handler or any decent way to make my skript react to changes in a text widget (e.g. a text is beign typed in or copy-pasted) while Mainloop is in action? The only thing I can think of is creating hotkeys via bind that only lead to an reaction if the text widget is in focus... any nicer way to do this? Thank you, dear monks Lodae
  • Comment on Onchange-like event handler for Tk text widget

Replies are listed 'Best First'.
Re: Onchange-like event handler for Tk text widget
by vkon (Curate) on Apr 11, 2011 at 22:25 UTC
    yes, the virtual event <<Modified>> is generated,
    here is one-liner:
    perl -MTcl::Tk -we '$t=Tcl::Tk::tkinit->Text->pack;$t->bind("<<Modifie +d>>"=>sub{if($t->editModified){print qq/chg\n/;$t->editModified(0)}}) +;Tcl::Tk::MainLoop'
    (I have no perl/Tk handy, so using Tcl::Tk CPAN module instead)
Re: Onchange-like event handler for Tk text widget
by lamprecht (Friar) on Apr 11, 2011 at 22:52 UTC

    Update: Please ignore - <<Modified>> works as advertised...

    There seems to be a bug on Win32 which keeps <<Modified>> from being generated in case of a Ctrl-V paste. Here is another way to trigger on modifications:

    use warnings; use strict; use Tk; my $mw = tkinit; $mw->Text; my $insert = \&Tk::Text::insert; *Tk::Text::insert = sub{changed($_[0]); $insert->(@_); }; my $t = $mw->Text->pack; #$t->bind('<<Modified>>',sub{print shift," changed\n"}); MainLoop; sub changed{ print "$_[0] changed\n"; }
    Cheers, Christoph
      I do not know what bug you're talking about,
      my code, rewritten adopted to Tk, behaves correctly for win32, including ctrl+v copy-pasting:
      use Tk; my $t=tkinit->Text->pack; $t->bind('<<Modified>>'=>sub { if($t->editModified) {print qq/chg\n/;$t->editModified(0)} }); MainLoop;

        You are right. I possibly did something stupid like trying to paste from an empty clipboard. Thanks for the clarification.

        Cheers, Christoph
        Dear Monks, thanks a lot. I'll try that. Greetings Lodae