use strict; use Tk; my $mw = MainWindow->new(); my $input_frame = $mw->Frame() ->pack(-expand => 1, -fill => 'both'); my $input = $input_frame->Scrolled('Text') ->pack(-expand => 1, -fill => 'both'); $input->tagConfigure("invisible", -elide => 'true'); my %buttons; for (my $i = 1; $i < 20; $i++) { if ($i % 5 == 0) { # Create both buttons $buttons{"minus_$i"} = $input->Button(-image => $mw->Getimage('minus'), -command => [\&hide,$i]); $buttons{"plus_$i"} = $input->Button(-image => $mw->Getimage('plus'), -command => [\&reveal,$i]); # Insert only the minus button $input->windowCreate('end',-window => $buttons{"minus_$i"}); } $input->insert('end',"abcdefghijkl$i\n"); } MainLoop(); sub hide { my ($index) = @_; my $from = $index + 1; my $to = $index + 5; $input->tagAdd("invisible","$from.0","$to.0"); switch_button($index,'plus','minus'); return; } sub reveal { my ($index) = @_; my $from = $index + 1; my $to = $index + 5; $input->tagRemove("invisible","$from.0","$to.0"); switch_button($index, 'minus', 'plus'); return; } sub switch_button { my ($j, $new_sign, $old_sign) = @_; $input->delete("$j.0"); if ($old_sign eq 'plus') { $buttons{"plus_$j"} = $input->Button(-image => $mw->Getimage('plus'), -command => [\&reveal,$j]); } else { $buttons{"minus_$j"} = $input->Button(-image => $mw->Getimage('minus'), -command => [\&hide,$j]); } $input->windowCreate("$j.0",-window => $buttons{"${new_sign}_$j"}); return; }