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; }
In reply to Tk : Entry widget-Strange behaviour with decimals by Caerwyn1955
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |