Perl TK - Wrap text in an Entry widget

489 Views Asked by At

I created a table that contains a "Key" column, "Value" column and a "New Value" column as shown in the image below. The "Key" and "Value" columns are implemented as Labels, the "Value" column is wrapped as you can see. The "New Value" column is implemented as an Entry widget because it should be editable. There is a Copy & Paste button that copies the value to the "New Value" Entry field. I would like to wrap the text in the Entry widget, so after pressing the button it will look like the text in the "Value" field.

Image that shows the table I built and the difference between the wrapped Label to the text in the Entry field

Here is the piece of code that defines the shown columns:

        my $key_label = $table->Label(-text => $key , -width => 50, -bg => $background_color, -anchor => 'w', -relief => $relief_style, -wraplength => 300)->pack(-side => 'left');
        $table->put($curr_row,1,$key_label);
        my $orig_val_label = $table->Label(-text => $full_cfg_hash{$key}{'old_value'}, -width => 50, -bg => $background_color, -anchor => 'w',  -relief => $relief_style, -wraplength => 300)->pack(-side => 'left');
        $table->put($curr_row,2,$orig_val_label);
        my $new_val_entry = $table->Entry(-text => $full_cfg_hash{$key}{'new_value'}, -width => $entry_size, -bg => $background_color)->pack( -side => 'left', -fill => 'both', -expand => 'yes');
        $table->put($curr_row,3,$new_val_entry);
        my $copy_paste_btn = $table->Button(-text => "Copy & Edit\nOld Value", -command => [\&copy_n_edit_old_value,$full_cfg_hash{$key}{'old_value'},$new_val_entry], -bg => $buttons_background, -foreground => $buttons_text_color)->pack(-side => 'left', -padx => 5);
        $table->put($curr_row,4,$copy_paste_btn);
1

There are 1 best solutions below

0
Ștefan On

The Tk::Text widget is for multi-line text entry, usually combined with Tk::Scrolled, something like:

my $new_val_entry = $table->Scrolled(
    'Text',
    -width      => 40,
    -height     => 3,
    -wrap       => 'word',
    -scrollbars => 'e',
    -font       => $my_font,
)->pack(
    -expand => 1,
    -fill   => 'both',
    -padx   => 5,
    -pady   => 5,
);