Those two updates obviously will not work the way you expected. They only get accessed after the downloading is completed. During the downloading, your GUI will just sort of "hang".
I have some script, sort of still "work in progress", but it helps you to attack this problem. What I did was that, I wrote the download part myself, instead of using any module so that I know the progress, and can update the progressbar every time after a portion of the downloading is done: (I am still working on it, so lots of stuffs are hardcoded, but the idea is certainly there. Take a look.)
use Tk; use Tk::Progressbar; use IO::Socket::INET; use IO::Select; use strict; use warnings; my $mw = MainWindow->new(); $mw->Button(-text => "Download", -command => \&download)->pack(); MainLoop; sub download { my @documents; for (1..20) { push @documents, "song$_.Wma"; } my $connections; my $selector = IO::Select->new(); while (1) { if (my @readers = $selector->can_read(5)) { for my $reader (@readers) { my $line; if (my $length = sysread($reader, $line, 2048)) { syswrite($connections->{$reader}->{"file"}, $line) +; $connections->{$reader}->{"received"} += $length; $connections->{$reader}->{"progress"}->value($conn +ections->{$reader}->{"received"} / $connections->{$reader}->{"length" +} * 100); $mw->update(); } else { close $connections->{$reader}->{"file"}; delete $connections->{$reader}; $selector->remove($reader); close $reader; } } } else { if (my $document = pop @documents) { if (my $connection = IO::Socket::INET->new(Proto => "t +cp", PeerAddr => "music.eting.cn", PeerPort => 80)) { binmode($connection); $selector->add($connection); print $connection "GET http://blah.com/$document H +TTP/1.1\r\nHost: 0.0.0.0\r\n\r\n"; my $line; my $offset = 0; while (1) { my $len = sysread($connection, $line, 2048, $o +ffset); print $line; if ($line =~ m/\r\n\r\n/) { last; } else { $offset += $len; } } $line =~ m/Content-Length: (\d*)/; print "$1\n"; $connections->{$connection}->{"length"} = $1; my $file; open $file, ">", $document; binmode($file); $line =~ m/.*?\r\n\r\n(.*)/s; syswrite($file, $1); $connections->{$connection}->{"received"} = length +($1); $connections->{$connection}->{"file"} = $file; $connections->{$connection}->{"document"} = $docum +ent; my $frame = $mw->Frame()->pack(); $frame->Label(-text => $document)->pack(); $connections->{$connection}->{"progress"} = $frame +->ProgressBar( -width => 20, -length => 200, -anchor => 'w', -from => 0, -to => 100, -blocks => 1, -colors => [0, 'green'])->pack(); $mw->update(); } } else { unshift @documents, $document; } } } }
In reply to Re: LWP::Simple causing Tk error
by pg
in thread LWP::Simple causing Tk error
by wook
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |