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

I'm prototyping an app that will display a file in a Text widget and allow you to apply filters to the text that will make the matching lines change attributes (color, bold, etc). One of the things that I would like the app to do is be able to make the lines disappear altogether, but easily be able to put them back if the filter is removed.

I get the attribute changing fairly cheaply by using tags and there is a -hidden flag that can be applied to tags, but from reading the source it seems as though hidden just means hidden from searches of the text, not hidden from the display. I've also tried finding a font that wouldn't display, but the only thing that I've found in X is nil2 which still leaves a 2 pixel high version of the line visible.

I have to admit that I'm lazy and am not too interested in putting the text into a canvas to accomplish this, but I would do that as a last resort.

Are there any ideas lurking these hallowed halls to help me continue the path of laziness?

  • Comment on Tk: Make lines disappear in a Text widget?

Replies are listed 'Best First'.
Re: Tk: Make lines disappear in a Text widget?
by converter (Priest) on Oct 09, 2003 at 17:46 UTC

    Invoke tagConfigure with the -state parameter, with a value 'hidden'. When this tag is added to a range, the text will not be displayed in the text widget. Invoking tagRemove with the tag name and range will make the text visible again.

    # A quick demo: hide the selected text, # press the Escape key to make hidden # text visible again $mw = tkinit; $t = $mw->Text->pack; $t->tagConfigure( 'hide', -state => 'hidden', ); $b = $mw->Button( -text => 'Hide', -command => \&hide_text, )->pack; $mw->bind('<Escape>', \&show_text); MainLoop; sub show_text { my @r = $t->tagRanges('hide'); return unless @r; $t->tagRemove('hide', @r); return; } sub hide_text { my @r = $t->tagRanges('sel'); return unless @r; $t->tagAdd('hide',@r); return; }
      Must be the version of Tk that I'm using. I'll update to the latest and try again.
Re: Tk: Make lines disappear in a Text widget?
by Courage (Parson) on Oct 09, 2003 at 17:54 UTC
    You should use -elide option on tags, not '-hidden'.
    Look (Win32 command line):
    perl -MTk -we "for(tkinit->Text->pack){$_->tagConfigure('hidd',-elide= +>1);$_->insert('end','x'x45,'','yyy','hidd');$_->update;sleep 1;$_->t +agConfigure('hidd',-elide=>0);}MainLoop"
    or even
    perl -MTcl::Tk=:perlTk -we "for(tkinit->Text->pack){$_->tagConfigure(' +hidd',-elide=>1);$_->insert('end','x'x45,'','yyy','hidd');update;slee +p 1;$_->tagConfigure('hidd',-elide=>0);}MainLoop"

    Courage, the Cowardly Dog
    things, I do for love to perl...

Re: Tk: Make lines disappear in a Text widget?
by ptkdb (Monk) on Oct 10, 2003 at 12:09 UTC
    Take a line you want to hide, change its configation tag to 'myHidden' or something. Configure the properties for 'myHidden' so that the foreground and background colors are the same as the background color for the Text Widget.
    use strict ; use Tk ; use Tk::Text ; my($mw) = new MainWindow ; my($txt) = $mw->Scrolled('Text', -background => 'white', -foreground => 'black')->pack(-expand => 1, - +fill => 'both') ; $txt->tagConfigure('myHidden', -foreground => 'white', -background => +'white') ; $txt->insert('end', "line1\n", 'default') ; $txt->insert('end', "line2\n", 'myHidden') ; $txt->insert('end', "line3 (what happened to line2??)\n", 'default') ; MainLoop ;