Hello again choroba,
I'd like to impose again on your generosity by asking a few detailed questions about your/my code. At issue for me are some public warnings in the Tk community, foremost from Active State, to wit: favor 'use Tkx' instead of 'use Tk' because the Perl module Tk has been allowed to languish even as Tcl/Tk continues to advance. The theory there, I infer, is that the Tkx wrapper makes calls directly into the Tcl/Tk library, assuming it is installed. I have Perl Tk and perl Tkx, so could use either. The Tkx to Tcl syntax is challenging but once you get the hang of using '_' and '__' it makes sense. However, do you concur that Tkx facilitates access to more recent changes in Tk than does Perl Tk?
As an exercise I ported your Tk solution to Tkx, as below. It looks exactly like your Tk version and yields the same answer, including 100! :-). I was just about to try sprintf() when I got your answer.
#!/usr/bin/env -S perl
##!/usr/bin/env -S perl -d
#
# Script to exercise Perl Tkx/Tcl bindings
#
use warnings;
use strict;
use v5.30;
use feature ":all";
use Math::BigInt;
use Tkx;
sub factorial {
my ($n) = @_;
return 1 if $n == 0;
no warnings 'recursion';
return factorial($n-1) * $n
}
my $mw = Tkx::widget->new(".",);
$mw->g_wm_title("Tkx Factorial Calculator");
my $result;
my $value;
my $e = $mw->new_entry(
-width => 6,
-relief => 'sunken',
-textvariable => \$value,
);
my $calculate = sub {
# Stringify to prevent the conversion to int or float!
say $value;
$result = "" . factorial(Math::BigInt->new($value));
say $result;
};
my $desc = $mw->new_label(-text => 'Factorial is:',);
my $r = $mw->new_label(-textvariable => \$result,);
my $blk = $mw->new_label(-text => '', -width => 8,);
my $calc = $mw->new_button(-text => 'Calculate', -command => $calculat
+e);
my $q = $mw->new_button(-text => 'QUIT', -command => sub { $mw->g_d
+estroy; },);
Tkx::grid($blk, $e, $desc, $r, -padx => 10, -pady => 10);
Tkx::grid($calc, -column=>3, -padx => 10, -pady => 10);
Tkx::grid($q, '-', '-', -padx => 10, -pady => 15);
$e->g_bind('<Return>', $calculate);
$e->g_focus();
Tkx::MainLoop;
__END__
Some comments/questions about this code:
Please notice my grid expressions. I wanted the Entry widget, $e, to appear in col2 but when I tried:
Tkx::grid("-", $e, $desc, $r, -padx => 10, -pady => 10);
the program failed to compile. I had to add that kludge $blk for 'blank' in order to push the 1st line of widgets to the right one position. I tried myriad -column, -row and -columnspan settings but could not duplicate what I was able to do with those 3 simple grid expressions. On macOS the Entry appears over the Quit, while the Calculate button is centered directly under the Result regardless how long the result string. Also, I omitted any geographic attachment/sticky settings and the default has the entire set of widgets locked in place when the window is resized; again, exactly what I was looking for and did not have to code.
So, I now, thanks to you have a model I can build on going forward, but I am troubled by this bias in the Perl/Tk community against Perl Tk and for Tkx. What really are the issues there? Also, can you point me at some additional documentation and examples of using grid? What I have found so far seems overly complicated compared to what you and then I did with this example.
Thanks again, Will
|