Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^4: Tk : Entry widget-Strange behaviour with decimals

by poj (Abbot)
on Jan 07, 2018 at 16:49 UTC ( [id://1206855]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Tk : Entry widget-Strange behaviour with decimals
in thread Tk : Entry widget-Strange behaviour with decimals

or advice on my little program, i'd be glad to hear it.

Since you asked, rather than using a lot of similar variable names like $row0, $row1 ,$row2 etc consider using an array. For example

my @col = (40,$center-35,$center+30,$breed-40); my @row = (20,40,60,80,90,110);

a few more changes (mainly style) here

#!perl use strict; use warnings; use 5.010; use Tk; printf "Tk::VERSION %s\n",$Tk::VERSION; # Declarations my $breed = 400; # width my $hoog = 200; # height my $netspdx = 1; my $netspdy = 0; my $netuse = 0.05; my $fsizex = 3; my $fsizey = 0; my $mw = new MainWindow( -height => $hoog, -width => $breed, -title => 'Downloadtime estimator' ); init(); MainLoop; sub init { my $center = $breed/2; my @col = (40,$center-35,$center+30,$breed-40); my @row = (20,40,60,80,90,110); my @speed = ( undef, ['Gb',1000000000], ['Mb',1000000], ['kb',1000], ); my @button = (); # Row 0 my $y = $row[0]; $mw->Label(-text=>"Fill in the fields and check the right boxes") ->place( -x=>$center, -y=>$y, -anchor=>'center' ); # Row 1 $y = $row[1]; $mw->Label(-text=>"Network speed") ->place( -x=>$col[0], -y=>$y, -anchor=>'w' ); $mw->Entry( -justify=>'right',-textvariable=> \$netspdx, -width=>5 ) ->place( -x=>$center, -y=>$y, -anchor=>'center' ); my $x = $col[2]; for my $i (1..3){ $button[$i] = $mw-> Radiobutton( -variable=> \$netspdy, -text => $speed[$i][0], -value => $speed[$i][1]) -> place( -x =>$x, -y=>$y, -anchor=>'w' ); $x += 50; } $button[1]->select(); # Row 2 $y = $row[2]; $mw->Label( -text=>"Avg network use in %" ) ->place( -x=>$col[0], -y=>$y, -anchor=>'w' ); $mw->Entry( -textvariable=> \$netuse, -width => 5, -justify => 'righ +t', ) ->place( -x=>$center, -y=>$y, -anchor=>'center' ); # Row 3 $y = $row[3]; $mw->Label( -text=>"File size" ) + ->place( -x=>$col[0], -y=>$y, -anchor=>'w' ); $mw->Entry( -textvariable=> \$fsizex, -width => 5, -justify=>'right' ) ->place( -x=>$center, -y=>$y, -anchor=>'center' ); $x = $col[2]; for my $i (1..3){ $button[$i+3] = $mw-> Radiobutton( -variable=> \$fsizey, -text => uc $speed[$i][0], -value => $speed[$i][1]) ->place( -x=>$x, -y=>$y, -anchor=>'w' ); $x += 50; } $button[4]->select(); # Row 5 $y = $row[5]; $mw->Label( -text=>"Estimated time" ) ->place( -x=>$col[0], -y=>$y, -anchor=>'w' ); my $result = $mw->Label( -text=>'' ) ->place( -x=>$center, -y=>$y, -anchor=>'center' ); my $units = $mw->Label( -text=>'' ) ->place( -x=>$col[2], -y=>$y, -anchor=>'w' ); # Row 6 $y = $hoog-50; # OK button $mw->Button( -text => "Okay", -width => 10, -command => [\&Calc,$result,$units] ) ->place( -x=>$center-100, -y=>$y ); # Exit button $mw->Button(-text => "Exit", -width => 10, -command => sub { exit }) ->place( -x=>$center+100, -y=>$y ); } sub Calc { my ($result,$units) = @_; my $time = $fsizex*$fsizey / ($netspdx*$netspdy/100*$netuse); my $text = 'Seconds'; if ($time > 60){ $time = $time / 60; $text = 'Minutes'; if ($time > 60){ $time = $time / 60; $text = 'Hours'; if ($time > 24){ $time = $time / 24; $text = 'Days'; } }; } $units->configure(-text=>$text); $result->configure(-text=>sprintf "%.2f",$time); };
poj

Replies are listed 'Best First'.
Re^5: Tk : Entry widget-Strange behaviour with decimals
by Caerwyn1955 (Novice) on Jan 07, 2018 at 18:48 UTC

    Wow!
    You really gave me something to study here.
    I started Perl just 8 days ago, and I did my last programming some 20 years ago.
    I see you are using things that i didn't know existed, or what they really do, like \%.
    Since i already know what my program is suppoded to do, i can now trace the use of those things.
    Your comment does indeed help me on my way, and will probably be keeping me busy for a number of days.
    Thanks.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1206855]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found