in reply to Re^3: Editable tree in Tk
in thread Editable tree in Tk

After some digging, I figured it out. I was deleting a button from the Text widget, as you'd delete text, but that destroys the button completely, so I got an error the next time I tried to re-insert it. It was still in my hash of buttons, though, which made me thing the button still exists. Took me a while to debug my sample code until I made it do what I want :)

If anyone's interested, I'm attaching my sample code. Now on to changing the original to behave the same way...

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('min +us'), -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('min +us'), -command => [\&hide,$j]); } $input->windowCreate("$j.0",-window => $buttons{"${new_sign}_$j"}) +; return; }

EDIT: I just checked out the code in your other reply, and though I think that for now I'll stay with my version, I definitely learned a couple of new things from it. Thanks!

Replies are listed 'Best First'.
Re^5: Editable tree in Tk
by zentara (Cardinal) on Dec 25, 2008 at 16:53 UTC
    Very nice. All you need is some colors,some tags that do indentation, and a mouse cursor change.

    I'm not really a human, but I play one on earth Remember How Lucky You Are