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

Hi Monks, I'm new to Perl/Tk but already ran into a strange problem. I made a program (source included) that accepts numbers with the entry widget. As long as i use integers, the program acts as expected, but when i use decimals, it gets weird. When the program is not stopped, and another calculation is made (with a decimal under 1 ie. 0.05) the calculation is performed correct, but re-running the program w/o a change, any change to the field with the decimal, results in an error message at the console. When the decimal is above 1 (ie. 1.5) the error message will not appear, but the calculated value changes. So i wonder if somehow, "entry" has a problem with retaining the value of decimals.

Error message:

Tk::Error: Illegal division by zero at C:\perl\dwnspd.pl line 79. main::__ANON__ at C:\perl\dwnspd.pl line 79 Tk callback for .button Tk::__ANON__ at C:/Strawberry/perl/site/lib/Tk.pm line 251 Tk::Button::butUp at C:/Strawberry/perl/site/lib/Tk/Button.pm line 17 +5 <ButtonRelease-1> (command bound to event)
use strict; use warnings; use 5.010; use Tk; #use Win32::Console; #Win32::Console::Free(); system("cls"); # declarations my $netspdx=1; my $netspdy=0; my $netuse=0.05; my $fsizex=3; my $fsizey=0; my $mw; my $breed=400; my $hoog=200; my $cent=$breed/2; # needs -anchor=>'center' my $tekst; my $button; my $but1; my $but2; my $but3; my $but4; my $but5; my $but6; my $entry1; my $entry2; my $entry3; my $col0=40; # need -anchor=>'w' my $col1=$cent-35; # need -anchor=>'w' my $col2=$cent+30; # need -anchor=>'w' my $col3=$breed-40; # need -anchor=>'w' my $row0=20; my $row1=40; my $row2=60; my $row3=80; my $row4=90; my $row5=110; initi(); $tekst=$mw -> Label(-text=>"Fill in the fields and check the righ +t boxes") -> place(-x=>$cent, -y=>$row0, -anchor=>'center'); $tekst=$mw -> Label(-text=>"Network speed") + -> place(-x=>$col0, -y=>$row1, -anchor=>'w'); $entry1=$mw -> Entry(-justify=>'right',-textvariable=> \$netspdx, +-width=>5) -> place(-x=>$cent, -y=>$row1, -anchor=>'center'); $but1=$mw -> Radiobutton(-text => 'Gb', -variable=>\$netspdy, -v +alue=>1000000000) -> place(-x=>$col2, -y=>$row1, -anchor=>'w'); $but1->select(); $but2=$mw -> Radiobutton(-text => 'Mb', -variable=>\$netspdy, -v +alue=>1000000) -> place(-x=>$col2+50, -y=>$row1, -anchor=>'w'); $but3=$mw -> Radiobutton(-text => 'Kb', -variable=>\$netspdy, -v +alue=>1000) -> place(-x=>$col2+100, -y=>$row1, -anchor=>'w'); $tekst=$mw -> Label(-text=>"Avg network use in %") + -> place(-x=>$col0, -y=>$row2, -anchor=>'w'); $entry2=$mw -> Entry(-justify=>'right',-textvariable=> \$netuse,-w +idth=>5) -> place(-x=>$cent, -y=>$row2, -anchor=>'center'); $tekst=$mw -> Label(-text=>"File size") + -> place(-x=>$col0, -y=>$row3, -anchor=>'w'); $entry3=$mw -> Entry(-justify=>'right',-textvariable=> \$fsizex,-w +idth=>5) -> place(-x=>$cent, -y=>$row3, -anchor=>'center'); $but4=$mw -> Radiobutton(-text => 'GB', -variable=>\$fsizey, -va +lue=>1000000000) -> place(-x=>$col2, -y=>$row3, -anchor=>'w'); $but4->select(); $but5=$mw -> Radiobutton(-text => 'MB', -variable=>\$fsizey, -va +lue=>1000000) -> place(-x=>$col2+50, -y=>$row3, -anchor=>'w'); $but6=$mw -> Radiobutton(-text => 'KB', -variable=>\$fsizey, -va +lue=>1000) -> place(-x=>$col2+100, -y=>$row3, -anchor=>'w'); $tekst= $mw -> Label(-text=>"Estimated time") -> place(-x=>$col0, +-y=>$row5, -anchor=>'w'); $tekst= $mw -> Label(-text=>"minutes") -> place(-x=>$col2, -y=>$ro +w5, -anchor=>'w'); endi(); # subroutines # init info sub initi{ $mw = new MainWindow(-height=>$hoog,-width=>$breed,-title=>'Downloadti +me estimator'); $mw -> geometry("+100+100"); } # end info sub endi { $button = $mw -> Button(-text => "Okay", -command => sub { + $tekst= $mw -> Label(-text=>" ") -> place(-x +=>$cent, -y=>$row5, -anchor=>'center'); # not very elegant, any ideas + ? $tekst= $mw -> Label(-text=> int( (($fsizex*$fsizey) / ((($netspdx*$netspdy)/100) *$netus +e)) /60 ) )-> place(-x=>$cent, -y=>$row5, -anchor=>'center'); }) -> place(-x=>$col0, -y=>$hoog-50, -anchor=>'w'); $button = $mw -> Button(-text => " Exit ", -command => sub { exit +}) -> place(-x=>$col3, -y=>$hoog-50, -anchor=>'e'); MainLoop; }

Replies are listed 'Best First'.
Re: Tk : Entry widget-Strange behaviour with decimals
by Athanasius (Archbishop) on Jan 06, 2018 at 08:01 UTC

    Hello Caerwyn1955, and welcome to the Monastery!

    I can reproduce your error message exactly under Tk version 804.033, but not under 804.034 (the latest version). I admit, the Tk Changes documentation has left me none the wiser as to an explanation for the error message in the earlier version.1 :-( Nevertheless, I think your first priority should be upgrading to the latest version of Tk and re-testing.

    BTW, in cases like this you should specify both the version of Tk and the version of Perl you are using, along with your OS/platform. That will make it easier for the monks to give you the best advice.

    Update: 1My thanks to choroba, whose solution of this mystery appears below.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      > Tk Changes documentation has left me none the wiser

      33_500 version Changes contains a suspicious item:

      > No special xOKp handling in Perl_Value (RT #121528)

      Searching for the bug shows it's indeed related:

      After tying a variable containing a float to an Entry box, NOT destroying the Entry box, and then multiplying the variable by 1.0, the variable is no longer treated as a float correctly.

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

        Very interesting indeed.
        Especially since it seems to be happening primarly with certain combinations of Perl and Tk versions.
        Funny to see that my initial suspicion was right, the entry()'d value is somehow converted to an integer.
        Concerning you other remark about versions, i looked it up, and i'll just put it ahead of my code from now on, starting each new program with:

        # Windows 7/64-bit
        # Perl 5.26.1.1 for MSWin32-x64-multithread
        # Tk 804.034

        So i won't forget to mention those anymore, and it's a good reminder for myself too.
        If you have any more remarks or advice on my little program, i'd be glad to hear it.
        J.

      To be frank, i don't have a clue what version of Tk i have, i thought i got the latest version (three days ago), but it didnt install, so i tried some stuff i found using google.
      That might have installed the pre-last version.
      I'll try to get the latest, and will include that kind of info the next time.

        To get the module version, type this from the command line:

        18:20 >perl -MTk -wE "say $Tk::VERSION;" 804.034 18:20 >

        (That’s under Windows. Under *nix, use single quotes around the code.)

        Or install Module::Version and then type:

        18:20 >mversion -f Tk Tk 804.034 18:20 >

        To get your Perl version, type either perl -v to get the summary, or perl -V (that’s an uppercase “V”) to get a lot of information about your Perl installation.

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        The update did the trick.
        Thank you, Oh Sensei !

Re: Tk : Entry widget-Strange behaviour with decimals
by Caerwyn1955 (Novice) on Jan 06, 2018 at 06:41 UTC

    I forgot to mention:
    It seems that the value with the decimal is rounded down after first use, as in: int()