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

rewrite to include complete app
use strict; use warnings; use Tk; my $mw = MainWindow->new( -bg=> "#000000", -borderwidth=> 0); $mw->geometry( '1000x600' ); my $LB = $mw->Scrolled ( 'ROText', -bg => '#C0C0C0', -fg => '#000000', -selectforeground => '#000000', -selectbackground => '#C0C0C0', -font => 'courier 18 normal', -relief => 'groove', -cursor => 'top_left_arrow', -scrollbars => 'e', -insertwidth => 0, -spacing1 => 1, -spacing2 => 1, -wrap => 'none', )->pack( -side => 'right', -expand => 1, -fill => 'both', ); sub markAccount { my $this = shift; my $f = $LB->tagCget("A$this", -font); printf "Mark: %d,%s\n",$this, $f; if (${$f} =~ /normal/i) { $LB->tag('configure', "A$this", -font => 'courier 18 norma +l', -foreground => '#000000', ); } else { $LB->tag('configure', "A$this", -font => 'courier 18 bold' +, -foreground => '#0000FF', ); } } sub InitializeListBox { $LB->delete('1.0','end'); for my $i (1..200) { $LB->insert('end', sprintf("%s\n",$i), "A$i" ); $LB->tag( 'bind', "A$i", '<Double-1>' => sub { markAccount($i +) } ); } } InitializeListBox(); MainLoop;



How do I reliably fetch a tag's "option" whether that option exists or not?


Secondly, How do I delete a particular option/value within tag?
ie, remove a single option: remove -font,
ie, remove all options: remove -font ..... foreground...




Thirdly, Does it even matter if I delete unused options or not? Will having unused option slows down the script?


thanks

Replies are listed 'Best First'.
Re: How to extract, delete TK tags' option/value
by tybalt89 (Monsignor) on Apr 20, 2018 at 14:22 UTC
    #!/usr/bin/perl # http://perlmonks.org/?node_id=1213205 use strict; use warnings; use Tk; use Tk::ROText; my $mw = MainWindow->new( -bg=> "#000000", -borderwidth=> 0); $mw->geometry( '1000x600' ); my $LB = $mw->Scrolled ( 'ROText', -bg => '#C0C0C0', -fg => '#000000', -selectforeground => '#000000', -selectbackground => '#C0C0C0', -font => 'Courier 16 normal', -relief => 'groove', -cursor => 'top_left_arrow', -scrollbars => 'e', )->pack( -side => 'right', -expand => 1, -fill => 'both', ); sub selectAccount { my $this = shift; print "select: $this\n"; $LB->tagConfigure("A$_", -background =>undef) for 1..200; $LB->tagConfigure("A$this", -background => 'yellow'); } sub markAccount { my $this = shift; print "mark: $this\n"; my $fontref = $LB->tagCget("A$this", -font); $LB->tagConfigure("A$this", -font => $fontref ? undef : 'Courier 1 +6 bold'); } sub InitializeListBox { $LB->delete('1.0','end'); for my $i (1..200) { $LB->insert('end', $i x 10, "A$i" ); $LB->tag( 'bind', "A$i", '<Button-1>' => sub { selectAccount( +$i) } ); $LB->tag( 'bind', "A$i", '<Double-1>' => sub { markAccount($i +) } ); $LB->insert('end', "\n" ); } } InitializeListBox(); MainLoop;
Re: How to extract, delete TK tags' option/value (as documented)
by Anonymous Monk on Apr 20, 2018 at 06:36 UTC

    However, doing so on a tag without any -font option, it even even bypasses the whole IF-THEN-ELSE statement. Now, how do I reliably fetch a tag's "option" whether that option exists or not.

    The answer to that is dot dot dot , same as the question :)

    Ok, a better answer might be, first wake up, then first write simple program that runs and dot dot dot don't put stuff in your program until you understand it

    Secondly, How do I delete a particular option/value within tag?

    configure undef

    Thirdly, Does it even matter if I delete unused options or not? Will having unused option slows down the script?

    Does wearing a mustache slow you down? If you're sitting down?

Re: How to extract, delete TK tags' option/value (Tk ROText indices index tagAdd tagRemove)
by Anonymous Monk on Apr 20, 2018 at 19:37 UTC

    The problem with this approach

    my $f = $LB->tagCget("A$this", -font); printf "Mark: %d,%s\n",$this, $f; if (${$f} =~ /normal/i) {

    When there is no font, then $f is not a reference, so program should first check if there is a reference

    But the real problem, why modify tag options when should be adding/removing tags? markAccount should toggle a 'markAccount' tag for ALine

    update:

    On every double click, toggle bolded for A1

    #!/usr/bin/perl -- use strict; use warnings; use Tk; use Data::Dump qw/ dd /; my $mw = Tk::MainWindow->new; $mw->geometry('400x300'); my $tw = $mw->Text(-wrap=>'word')->pack(qw/ -expand 1 -fill both /); $tw->insert( 'end', q{"There must be some kind of way out of here," Said the joker to +the thief.}, 'A1', ); $tw->tag( 'bind', 'A1', '<Double-1>', \&AnDouble ); $tw->tagConfigure( 'bolded', -font => 'courier 18 bold' ); MainLoop; sub AnDouble { #~ my( $tw ) = @_; #~ my $tw = $Tk::widget; my $tw = $Tk::event->W; my $mouseIndex = $tw->index( sprintf '@%s,%s',$Tk::event->x , $Tk: +:event->y ) ; my @tags = $tw->tagNames( $tw->index('A1.first') ); dd( [ $mouseIndex => @tags ], map { [ $_ => $tw->tagRanges($_) ] } + $tw->tagNames ); if( grep /bolded/, @tags ){ $tw->tagRemove('bolded', $tw->tagRanges('A1') ); } else { $tw->tagAdd('bolded', 'A1.first','A1.last' ); } } __END__ $ perl tk-text-toggle-tagadd.pl ([1.9, "A1"], ["sel"], ["A1", "1.0", 1.74], ["bolded"]) ( [1.4, "A1", "bolded"], ["sel", 1.1, 1.11], ["A1", "1.0", 1.74], ["bolded", "1.0", 1.74], )