perlmodal-dialogtk-toolkittext-width

Perl Tk::Dialog text width not matching dialog width


I am using Perl with Tk::Dialog.

I can't for the life of me work out how to set the width of the text box inside the dialog box to the width of the text it contains.

Failed attempt:

use strict;
use warnings;
use Tk;
use Tk::Dialog;

my $text1 = "This is the first answer which is a reasonably long string of text.";
my $text2 = "This is the second answer which is not quite as long as the first.";
my $textwidth = max(length($text1),length($text2)); # <-- get max text width here
    
# Create main window first (documentation/examples indicate this is necessary)
my $mw = MainWindow->new();
# create dialog
my $dialog = $mw->Dialog(-text           => "Choose answer:\n\n"
                         . "A) ".$text1."\nB) ".$text2,
                         -bitmap         => 'question',
                         -title          => 'Make a choice',
                         -default_button => 'Both',
                         -width          => $textwidth, # <-- set dialog to text width here
                         -buttons        => [qw/Left Right Both Cancel/]);
my $answer = $dialog->Show();
$mw->withdraw();

print $answer;

exit;

sub max {
    my ($max_so_far) = shift @_;
    foreach (@_) {
        if ($_ > $max_so_far) {
            $max_so_far = $_;
        }
    }
    return $max_so_far;
}

which results in: $dialog->Show()

The dialog window itself appears to be sized according to the text width, but the text field contained within (which also does not show borders) does not get resized. It seems to be fixed at around 35-40 characters wide (font depending).


Solution

  • Don't mess with character counts. You can simply use the -wraplength option here. -wraplength=>0 means 'do not wrap at all'. The widgets default -wraplength is hardcoded to '3i' (3 inch), you can also use a postfix c for cm values:

    use 5.032;
    use strict;
    use warnings;
    use Tk;
    use Tk::Dialog;
    my $text1 = "This is the first answer which is a reasonably long string of text.".'x'x 30;
    my $text2 = "This is the second answer which is not quite as long as the first.";
    
    # Create main window first (documentation/examples indicate this is necessary)
    my $mw = MainWindow->new();
    # create dialog
    my $dialog = $mw->Dialog(-text           => "Choose answer:\n\n"
                             . "A) ".$text1."\nB) ".$text2,
                             -bitmap         => 'question',
                             -title          => 'Make a choice',
                             -default_button => 'Both',
                             -wraplength     => 0, #'6i' '15c'
                             -buttons        => [qw/Left Right Both Cancel/]);
    my $answer = $dialog->Show();
    $mw->withdraw();
    
    print $answer;
    

    See https://metacpan.org/dist/Tk/view/pod/options.pod#Name:-wrapLength

    If you want to use your own logic, you have to take into account the actual font used and screen resolution etc. Tk::Font has $widget->fontMeasure(font,text) to calculate pixel width of a string in a given widget.