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

Hello,

Is there a widget in Tk similar to Tree, but which allows the user to edit the text in the entries?

If not, what would be the best way to implement something of the sort? I thought of using Text, and adding plus/minus images at the appropriate places, which would hide/show some of the text. Of course, I'd appreciate other suggestions.

Thanks!

Anna.

Replies are listed 'Best First'.
Re: Editable tree in Tk
by zentara (Cardinal) on Dec 25, 2008 at 12:53 UTC
    If you are willing to move up to Perl/Gtk2, it has a nice editable treeview.
    #! /usr/bin/perl -w use strict; use Gtk2 '-init'; use Glib qw/TRUE FALSE/; #standard window creation, placement, and signal connecting my $window = Gtk2::Window->new('toplevel'); $window->signal_connect('delete_event' => sub { Gtk2->main_quit; }); $window->set_border_width(5); $window->set_position('center_always'); #this vbox will geturn the bulk of the gui my $vbox = &ret_vbox(); #add and show the vbox $window->add($vbox); $window->show(); #our main event-loop Gtk2->main(); ###################################################################### +## sub ret_vbox { my $vbox = Gtk2::VBox->new(FALSE,5); $vbox->set_size_request (300, 300); #this is one of the provided base Gtk2::TreeModel classes. my $tree_store = Gtk2::TreeStore->new(qw/Glib::String/); #fill it with arbitry data foreach (1..3) { my $parent_nr = $_; my $iter = $tree_store->append(undef); $tree_store->set ($iter,0 => "Parent $parent_nr"); foreach (1..3){ my $iter_child = $tree_store->append($iter); $tree_store->set ($iter_child,0 => "Child $_ of Parent $pa +rent_nr"); } } #this will create a treeview, specify $tree_store as its model my $tree_view = Gtk2::TreeView->new($tree_store); #create a Gtk2::TreeViewColumn to add to $tree_view my $tree_column = Gtk2::TreeViewColumn->new(); $tree_column->set_title ("Click to sort"); my $renderer = Gtk2::CellRendererText->new; $renderer->set_property('editable' => TRUE); $renderer->signal_connect (edited => \&cell_edited, $tree_ +store); $tree_column->pack_start ($renderer, FALSE); $tree_column->add_attribute($renderer, text => 0); #add $tree_column to the treeview $tree_view->append_column ($tree_column); $vbox->pack_start($tree_view,TRUE,TRUE,0); $vbox->show_all(); return $vbox; } sub cell_edited { my ($cell, $path_string, $new_text, $model) = @_; my $path = Gtk2::TreePath->new_from_string ($path_string); my $column = $cell->get_data ("column"); my $iter = $model->get_iter ($path); $model->set_value ($iter, $column, $new_text); }

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

      Unfotunately, I'm limited by what's installed at work... So Gtk2 is not an option.

      I actually found that embedding tree-like buttons into a Text widget is good enough for my purposes. Now I just have to figure out what I'm doing wrong when I'm trying to replace the button by a different one... :)

        Show a simple example and we might figure out the problem.

        I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Editable tree in Tk
by zentara (Cardinal) on Dec 25, 2008 at 15:29 UTC
    Here is a start on a editable Tree with Tk::Tree. I just hardcode 'foobar' in on a right click, but you would want to pop a small dialogbox with an entry, get the new text, then change the text.
    #!/usr/bin/perl use strict; use warnings; use Tk; use Tk::Tree; my($icon)=<<'END'; /* XPM */ static char * junk_xpm[] = { "10 10 6 1", " c #000000", ". c #FFFFFF", "X c #B129F8", "o c #F869A6", "O c #00FF00", "+ c #1429F8", " X..oo.+. ", " .X....+. ", " ..X...+. ", " o..X.... ", " oo..X... ", " .oo..X.. ", " ......X. ", " .+.....X ", " .+..oo.. ", " .+...oo. "}; END my $mw = MainWindow->new(-title=> 'Tree'); my $tree = $mw->Scrolled('Tree',-header=>'1',-scrollbars=>'osoe',- itemtype=>'imagetext',-separator => '|')->pack +; $tree->add('hi',-text=>"hi",-bitmap=>'questhead'); $tree->indicatorCreate('hi'); $tree->setmode('hi','close'); $tree->add('hi|there',-text=>"hi there",-image=>$mw->Pixmap(-data=>$ic +on)); $tree->indicatorCreate('hi|there'); $tree->setmode('hi|there','close'); $tree->add('hi|there|a',-text=>"hi there",-image=>$mw->Pixmap(-data=>$ +icon)); $tree->indicatorCreate('hi|there|a'); $tree->setmode('hi|there|a','close'); $tree->add('hi|there|a|c',-text=>"hi there",-image=>$mw->Pixmap(-data= +>$icon)); $tree->add('hi|there|b',-text=>"hi there",-image=>$mw->Pixmap(-data=>$ +icon)); $tree->bind("<Button-3>" => \&show_pos); MainLoop; sub show_pos { my ($x, $y) = $mw->pointerxy; print "$x $y\t"; my $s = $tree->nearest($y - $tree->rooty); print "$s\n"; $tree->selectionClear(); $tree->selectionSet($s); #pop a dialogbox here and get new text $tree->itemConfigure($s,0,-text=>'foobar'); }

    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Editable tree in Tk
by Anonymous Monk on Dec 25, 2008 at 09:07 UTC

      I'm familiar with HList, but that's not what I'm looking for. Once you populated the list, you can't change the text in the entries through the GUI itself, only through the code. I need to be able to edit the text from the outside.

        Once you populated the list, you can't change the text in the entries through the GUI itself, only through the code.
        That means that you can edit it through GUI, you just have to write some code. Simplest way is bind Alt+Click or something available, and popup a dialog asking for new value.