This is just a little demonstration program to show how to
use LWP to download a file, with a gtk toolkit progress indicator. 
It likes full url's , but will try to save index.html
files with the saved filename being the stripped url name.
#!/usr/bin/perl -w use strict; use Gtk; use LWP::UserAgent; set_locale Gtk; init Gtk; my $true = 1; my $false = 0; my ($window,$button,$vbox,$hbox,$label,$label1, $entry,$adj,$pbar,$URL,$filename); # Create the window. $window = new Gtk::Window( 'toplevel' ); $window->signal_connect( 'delete_event', sub { Gtk->exit( 0 ); } ); $window->border_width(10); $vbox = new Gtk::VBox( $false, 0 ); $hbox = new Gtk::HBox($false,0); $window->add($vbox); # Label to let the user know what is going on. $label = new Gtk::Label("--------------------Gtk downloader----------- +---------"); $vbox->pack_start( $label, $false, $false, 10 ); $label->show(); # Label to display URL to retreive. $label1 = new Gtk::Label( "file to retrieve\n" ); $vbox->pack_start( $label1, $false, $false, 10 ); $label1->show(); # Create the Entry $entry = new Gtk::Entry(100); $entry->signal_connect( "activate",\&get_entry); $entry->set_text( "Enter" ); $entry->append_text( "Url" ); $entry->select_region( 0, length( $entry->get_text() ) ); $vbox->pack_start( $entry, $true, $true, 0 ); $entry->show(); # Set up the progress bar $adj = new Gtk::Adjustment( 0, 1, 100, 0, 0, 0 ); $pbar = new_with_adjustment Gtk::ProgressBar( $adj ); $vbox->pack_start( $pbar, $false, $false, 10 ); $pbar->set_format_string("%p%%"); $pbar->set_show_text(1); $pbar->set_value(1); $pbar->show(); #Create start button $button = new Gtk::Button( "Start" ); $button->signal_connect( "clicked", \&get_file); $hbox->pack_start($button, $true, $false,0); $button->show(); # Create the close button $button = new Gtk::Button( "Close" ); $button->signal_connect( 'clicked', sub { Gtk->exit( 0 ); } ); $hbox->pack_start( $button, $true, $false,0); $button->show(); $vbox->add($hbox); $hbox->show(); $vbox->show(); $window->show(); main Gtk; exit( 0 ); ################################################################ sub get_file{ get_entry(); my $ua = LWP::UserAgent->new; open(IN,">$filename"); my $value = 0; my $expected_length; my $bytes_received = 0; my $res = $ua->request(HTTP::Request->new(GET => $URL), sub { my ( $chunk, $res ) = @_; $bytes_received += length($chunk); unless ( defined $expected_length ) { $expected_length = $res->content_length || 0; $label1->set_text( "Getting $filename\n$expected_length by +tes" ); }if($expected_length){ $value= int (100 * $bytes_received / $expected_lengt +h) || 0; } # write to the file print IN $chunk; $pbar->set_value( ++$value ) if ($value =~ /^[0-9]+$/); # Run the main loop as long as events are pending Gtk->main_iteration while ( Gtk->events_pending ); }); close IN; # Download is done, inform the user. if(-s $filename eq 0){ $label1->set_text( "file contained no data\nCheck URL" ); unlink $filename; }else{ $label1->set_text( "$filename saved\n Download Complete" ); $pbar->set_value(101); } return; } sub get_entry{ $URL = $entry->get_text(); $pbar->set_value(0); $filename = substr("$URL", rindex("$URL", "/" )+1); $filename =~ s/~//g; $filename = $filename || $URL; $filename =~ s/[~\/]//g; $label1->set_text("$filename\n"); return; }

In reply to download progress with gtk by zentara

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.