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

Hi Monks, I want to copy all the contents of one column of the table and paste it to the other column of the table, created in Perl/Tk. I also want to copy the contents of one column by dragging the mouse only. Code Snippet is as follows:
#!/usr/local/bin/perl use Tk; use Tk::Table; use Tk::Entry; $mw = new MainWindow; $title = $mw -> title("kapsule"); $show_table_frame = $mw->Frame()->pack(-side => "top",-fill => "both" +,-anchor => "nw",-padx=>"10"); $button = $mw-> Button(-text=>"show",-font => "verdanafont 10 bold",- +command=>sub { if ($show_table_frame ->ismapped) { $show_table_frame ->packForget(); } else { $show_table_frame ->pack(); } } )->pack(); $show_table = $show_table_frame->Table(-columns => 3,-rows => + 5,-scrollbars => "o",-fixedrows => 1,-fixedcolumns => 0,-relief => 'raised',-takefocus=>"0",-pady=>"5"); $tmp_label = $show_table->Label(-text => "Col. 1 ", -width => + 15, -relief =>'ridge'); $show_table->put(0, 1, $tmp_label); $tmp_label = $show_table->Label(-text => "Col. 2", -width => +15, -relief =>'ridge'); $show_table->put(0, 2, $tmp_label); $tmp_label = $show_table->Label(-text => "col. 3", -width => +15, -relief =>'ridge'); $show_table->put(0, 3, $tmp_label); for($col=1;$col<4;$col++) { for ($row=1;$row<5;$row++) { $ent = $show_table -> Entry(-font=>"verdana 10") -> + pack(-ipady=>"15"); $show_table->put($row,$col,$ent); } } $show_table->pack(); MainLoop;
Please help me in achieving this. Thanks in advance
  • Comment on Perl/Tk: How to select all and copy/paste all contents of one column of a table ?
  • Download Code

Replies are listed 'Best First'.
Re: Perl/Tk: How to select all and copy/paste all contents of one column of a table ?
by zentara (Cardinal) on Aug 25, 2008 at 14:17 UTC
    I have to give you points for hubris, asking for all that functionality out of a simple Table widget. :-) But down to business:

    First, always use warnings and use strict. It catches alot of little errors.

    Second, you probably need to change widgets, to do what you want. It seems like a simple question, but it is full of complexities, especially the dragging-all-selected with the mouse to copy, which I will not even address, unless you want to switch to a Canvas widget.

    First, the Tk::Table widget is very limited in functionality. There is no selection mechanism or type, so you would have to do all the row-col juggling yourself. This is how you would approach it. Change your column and row headers to buttons, and in their command callbacks, you will need to loop thru all the appropriate cells, and change their color to what you want to be "selected color". Then setup another mouse binding, say "Button 3" , a right click, so that whatever column header you click on after a selection has been made, will loop thru all the selected entries of the source row, and insert them into the appropriate entries in the target row. I only show a print here, I leave the work to you..... it's alot of details.

    See second example below for another widget you might try.that has selection built in. You can use the shift-button1 to select sections of columns or rows, or entire rows or columns by clicking the headers. The copying will still be a tedious task of looping thru all cells, and transferring them 1 by 1.

    Finally the third example, is a drag-n-drop. I include it to give you a clue as to how difficult it is to move data between separate widgets. It use an HList. The HList, Listbox, and Text widgets, are about the only Tk widgets that can do small amounts of crude drag-n-drop. Like I said, if you really need it, (DnD), go with a Canvas type widget, build your own cell-table, and drag all your want. DnD is preety hairy and bug-prone, which is why you see so little of it in use.


    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Perl/Tk: How to select all and copy/paste all contents of one column of a table ?
by kapsule (Acolyte) on Aug 25, 2008 at 06:36 UTC
    Should I bind key sequence Control+A to select the data columns wise in a table? how? But I think that wont be a god idea..because it will select all the table. or any other idea...to copy-paste column wise. Please help Monks!!