in reply to Re: Perl/Tk int overflow in Label widget
in thread Perl/Tk int overflow in Label widget

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

Replies are listed 'Best First'.
Re^3: Perl/Tk int overflow in Label widget
by choroba (Cardinal) on Nov 07, 2023 at 22:59 UTC
    Sorry, I have no experience with Tkx, I've only used Tk. What I know is that the original creator and maintainer of Tk has passed away, and that Tk is stuck with an old version of tcl/tk, missing some new widgets etc. My assumption was the problem would be the same, and I was correct.

    Regarding grid, I usually use the pack geometry manager, so I can't help you much, either. Sorry to disappoint you; hopefully another monk will be more knowledgeable.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]