in reply to Using Tk::Text and '<<Modified>>'

Yeah, you need to reset the $text->editModified(0) flag. The "gotcha" comes when you try to reset the flag automatically at the end of the <<Modified>> callback. You will get an infinite loop. So you need to manually reset it somehow, maybe with a timer?
#!/usr/bin/perl use warnings; use strict; use Tk; use Tk::Text; my $MW = MainWindow->new( -title => "Tk::Text test", -width => 200, -height => 200 ); my $text = $MW->Text( -height => 10, -width => 40, -wrap => 'word' ); $text->pack( -side => 'top', -fill => 'both' ); $text->bind( '<<Modified>>' => sub { getText( $text, @_ ); } ); $MW->Button(-text=>'Clear Modified Flag', -command => sub{ $text->editModified(0) } )->pack(); MainLoop; sub getText { my ( $t, @args ) = @_; my $text = $t->get( '0.0', 'end' ); print "Got: $text\n"; return if ( !$text ); return 1; }

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

Replies are listed 'Best First'.
Re^2: Using Tk::Text and '<<Modified>>'
by Courage (Parson) on Nov 20, 2004 at 15:44 UTC
    you're right.

    But to clear said flag, you could use something like following: (I checked, it worked):

    my $allowed=1; $text->bind('<<Modified>>' => sub { getText($text, @_); do {$allowed=0;$text->editModified(0);$allowed=1} if $ +allowed; } );

    unfortunately this is not simple, so not very Perlish.

    addition: surprisingly, afterIdle(...) do not resolve that, and its usage restores initial problem...

    Best regards,
    Courage, the Cowardly Dog

Re^2: Using Tk::Text and '<<Modified>>'
by castaway (Parson) on Nov 20, 2004 at 16:08 UTC
    *Boggle* That's um, weird.. One of these days someone is going to write a consistent interface for all the Tk:: modules. (Or, pigs might fly..)

    Is it just me, or does that all seem just a little strange?

    C.

      "a consistent interface for all the Tk:: modules."

      What is inconsistent here? I agree that it would be much better if they can add some examples here and there in Tk documents, so that we can reach the proper use more quickly. The entire Tk document is long but poor.

      But your issue really does not point me to anything that is "inconsistent", but rather some sort of learning curve.

      If you want me to point out the inconsistancy, I would say that they better support editModified() in widgets that are really similar to Tk::Text, for example Tk::Entry, but it does not.

      use Tk; use Tk::Dialog; use warnings; use strict; my $MW = MainWindow->new(-title => "Tk::Text test", -width => 200, -height => 200); my $text = $MW->Entry( -width => 40); $text->pack(-side => 'top', -fill => 'both'); $text->bind( '<FocusOut>' => \&callback); my $text2 = $MW->Text(-height => 10, -width => 40, -wrap => 'word'); $text2->pack(-side => 'top', -fill => 'both'); MainLoop; sub callback { if ($text->editModified()) { $text->Dialog(-title=>"Modified",-text=>"\$text has been modif +ied")->Show();#or whatever you want $text->editModified(0); } }
        Among the modules I am currently using, the methods to get/set their contents, or be notified when they change, vary wildly. Eg:
        • CheckButton -> set a -command callback, and call ->{Value} on it.
        • Entry -> set -validate and -vcmd, do a cget -textvariable and set/retrieve from that
        • BrowseEntry -> -browsecmd, fetch the entry subwidget then as above
        • Button -> -command and cget/configure -text
        • Text -> bind to something (focusout/modified), call -get('0.0', 'end') or somesuch to fetch, Contents() to set
        ... I'm sure I've missed some
        This to me is inconsistent. Why isn't there a 'get()' and 'set()' for all of these, together with a standard callback option to be called when they change? They could still have all their idiocyncraciesspecialities additionally if needed.

        Anyway, this is one of the major arguments that people keep giving me to try and persuade me to switch to some other GUI system, as Tks worst side.. And I keep coming across things that make me agree.

        BTW, I'd prefer Tk::Text be configured more like Tk::Entry and the rest, ie a -command or something on create, instead of the extra bind command, but thats probably just because all the widgets Im currently using also work that way.

        C.