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

The script below is a crude attempt to adds bullets to selected lines in a Text widget.

Is there was a way to make the bullets read only? If the bullets need to be removed the format (tags) should be removed (sub not shown) not by just deleting the bullets.

What is the best way to do this?

#!/bin/perl5 use strict; use warnings; use Tk; my $mw = MainWindow->new(-title => 'bullets'); my $t = $mw->Text->pack; { local $/ = undef; $t->insert('1.0', <DATA>); } $t->tagConfigure( 'bullets', -lmargin1 => '30', -lmargin2 => '61', ); $mw->Button(-text => 'bullet', -command => \&bullets)->pack; MainLoop; sub bullets{ unless ($t->tagRanges('sel')){ print "Can't apply bullets - no text selected\n"; return; } my $first_index = $t->index('sel.first'); my $last_index = $t->index('sel.last'); my $first_line = line_number($first_index); my $last_line = line_number($last_index); $t->unselectAll; for my $line ($first_line..$last_line){ $t->GotoLineNumber($line); $t->insert("$line.0", "* "); } $t->tagAdd('bullets', "$first_index linestart", "$last_index lineen +d"); return; } sub line_number{ my $index = shift; my ($line) = $index =~ /^(\d+)\./; return $line; } __DATA__ Twas brillig, and the slithy toves Did gyre and gimble in the wabe: All mimsy were the borogoves, And the mome raths outgrabe.

Replies are listed 'Best First'.
Re: Tk::Text widgets and bulleted lists
by zentara (Cardinal) on Dec 28, 2004 at 11:14 UTC
    Best way? I couldn't say, but similar questions have been asked before, and generally you should switch tactics, and use a ROText widget instead. That way you control all aspects of the text, and tags, when you click the update button. It's easier to have everything read-only, and control what changes can be made, rather than trying to "protect" something by tagging it.

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