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

Hi, How can I delete text in my text widget from Tk module?

Replies are listed 'Best First'.
Re: Tk - text widget - delete text
by Shendal (Hermit) on Oct 03, 2000 at 21:35 UTC
    Use the delete method. For example:
    $txtWidget = $Window->Scrolled("Text"); $txtWidget->insert('end',"blah blah blah blah"); $txtWidget->delete('0.0','end');
    This will delete all the text in your text box. If you'd like to delete just a section of it, you'll want to specify a start and end index.

    This and other Tk methods are documented in the Tk perldocs.

    Cheers,
    Shendal

    Update: In my haste, I made a mistake. It has been corrected above. $txtWidget->delete(0,'end') should be $txtWidget->delete('0.0','end').
      Thank you for reply
      Although, I get this:

      Tk::Error: bad text index "0" at C:/Perl/site/lib/Tk.pm line 217.
      Tk callback for .frame11.frame.text
      Tk::__ANON__ at C:/Perl/site/lib/Tk.pm line 217
      Tk::Derived::Delegate at C:/Perl/site/lib/Tk/Derived.pm line 469
      Tk::Widget::__ANON__ at C:/Perl/site/lib/Tk/Widget.pm line 311
      main::ClearList_Button at C:\Valeriy\Perl\Spiders\WebCrawler\WC3\WC33.PL line 2 14
      main::__ANON__ at C:\Valeriy\Perl\Spiders\WebCrawler\WC3\WC33.PL line 105
      \&main::__ANON__
      Tk callback for .frame10.button
      Tk::__ANON__ at C:/Perl/site/lib/Tk.pm line 217
      Tk::Button::butUp at C:/Perl/site/lib/Tk/Button.pm line 111 (command bound to event)
        Here's a more complete example:
        #!perl -w use strict; # always use Tk; my $Window = MainWindow->new(-title => "Test Window"); my $uframe =$Window->Frame() ->pack(-side => 'top', -fill => 'both', -expand => 1); my $dframe =$Window->Frame() ->pack(-side =>'top', -fill => 'x'); my $txtWidget = $uframe->Scrolled("Text") ->pack(-side => 'top', -fill => 'both', -expand => 1); my $pasteBtn = $dframe->Button(-text => "Paste", -command => sub { $txtWidget->insert('end',"blah b +lah") }) ->pack(-side => 'left'); my $clearBtn = $dframe->Button(-text => "Clear", -command => sub { $txtWidget->delete('0.0','end') } +) ->pack(-side => 'left'); MainLoop();

        Cheers,
        Shendal
RE: Tk - text widget - delete text
by softeng2000 (Novice) on Oct 03, 2000 at 21:57 UTC
    I got this info but I am not guru and cant do it. please help! :(

    ----
    linestart

    Adjust the index to refer to the first character on the line.
    lineend

    Adjust the index to refer to the last character on the line (the newline).

    $text->delete(index1, ?index2?)

    Delete a range of characters from the text. If both index1 and index2 are specified, then delete all the characters starting with the one given by index1 and stopping just before index2 (i.e. the character at index2 is not deleted). If index2 doesn't specify a position later in the text than index1 then no characters are deleted. If index2 isn't specified then the single character at index1 is deleted. It is not allowable to delete characters in a way that would leave the text without a newline as the last character. The command returns an empty string.
    ------

    I write:
    $text1->delete(linestart, lineend);

    and get error:
    bad text index "linestart" at C:/Perl/site/lib/Tk.pm line 217.

    Help please :)

Re: Tk - text widget - delete text
by softeng2000 (Novice) on Oct 03, 2000 at 22:14 UTC
    Ij, men. I think that is because I use Scrolled widget for texts. Thanks.