Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

making a GUI for a web client

by dannoura (Pilgrim)
on Jul 14, 2004 at 09:52 UTC ( [id://374252]=perlquestion: print w/replies, xml ) Need Help??

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

hi,

I'm trying to construct a GUI for a web client I have. The web client already works, and I can code the GUI, but I can't figure out how to tie them together. For example I have a retry sub which tries to load the webpage again in case of failure. I'd like it to report its status to the GUI window. The code for the GUI and the sub is below:

######## Tk stuff ######## my $mw=new MainWindow; $mw->minsize(qw(250 150)); $mw->title(" Tk Test 1 "); my $body=$mw->Frame(-background=>'cyan')->pack(side=>'bottom', -fill=> +'x'); my $fr1=$body->Frame(-background=>'magenta')->pack(side=>'top', -fill= +>'x'); $fr1->Label(-text=>'Hi', -background=>'yellow', -foreground=>'red')->p +ack(-fill=>'y'); # This label is where I want the status line to go t +o MainLoop(); ######## web client stuff ######## sub retry { # Retry downloading page in case of failure my $ua=shift; my $url=shift; for (my $i=1; $i<101; $i++) { sleep 5; # Delay my $request= $ua->request(GET "$url"); return $request if ($request->is_success); print "Download failed: ", $request->status_line, " Retrying $ +i... \n"; # I'd like all the print statements to go to the label die ("Download failed: ", $request->status_line) if ($i==101) +; } }

Can anyone give me any ideas?

-----------------------------------

Any comments about coding style are welcome.

Replies are listed 'Best First'.
Re: making a GUI for a web client
by friedo (Prior) on Jul 14, 2004 at 10:53 UTC
    In order for your retry function to report its status back to the window, you'll need to give it a callback function, or simply pass a reference to the window itself. Assuming retry is called from, say a button push, then you could pass $fr1 to it as a parameter, e.g.

    retry($ua, $url, $fr1); ... sub retry { # Retry downloading page in case of failure my $ua=shift; my $url=shift; for (my $i=1; $i<101; $i++) { sleep 5; # Delay my $request= $ua->request(GET "$url"); return $request if ($request->is_success); $fr1->Label(-text=>'Download failed: ' . $request->status_line +, -background=>'yellow', -foreground=>'red')->pack(-fill=>'y'); .....
    NOTE: untested.

    You get the idea. Any object you can pass to retry you can mess with in the function itself.

    However I would prefer passing retry a code reference which it can call with a status message. That way, it could report its status to anything; a GUI, a Curses interface, etc, depending on what that callback sub does.

      Thanks. Unless there's something I'm overlooking, this code produces a new label at each iteration. How do I fix that?

Re: making a GUI for a web client
by keszler (Priest) on Jul 14, 2004 at 10:54 UTC
    Assign a textvariable to the label. Anywhere you currently print, set the textvariable instead. You'll have to make the textvariable global, or pass it to the subroutine.

    You may want to $mw->update; to see the change immediately.

    $fr1->Label(-textvariable=>\$lblStatus, -background=>'yellow', etc. ... $lblStatus = "Download failed: " . $request->status_line . " Retrying +$i... \n"

      Thanks for the reply. I may be going the wrong way about this but using the code below doesn't update the label text, it produces another label.

      #! c:\perl\bin -w use strict; use Tk; my $mw=new MainWindow; $mw->minsize(qw(250 150)); $mw->title(" Tk Test 1 "); my $body=$mw->Frame(-background=>'cyan')->pack(side=>'bottom', -fill=> +'x'); my $fr1=$body->Frame(-background=>'magenta')->pack(side=>'top', -fill= +>'x'); my $txt='hi'; $fr1->Label(-textvariable=>\$txt, -background=>'yellow', -foreground=> +'red')->pack(-fill=>'y'); sleep 5; change('foo', $fr1, $mw); sleep 5; change('bar', $fr1, $mw); MainLoop(); sub change { my ($txt, $fr1, $mw)=@_; $fr1->Label(-textvariable=>\$txt, -background=>'yellow', -foregrou +nd=>'red')->pack(-fill=>'y'); $mw->update; }

      -----------------------------------

      Any comments about coding style are welcome.

        I'm not familiar with Tk, but I think he was trying to get you to do something more like this:

        #! c:\perl\bin -w use strict; use Tk; my $mw=new MainWindow; $mw->minsize(qw(250 150)); $mw->title(" Tk Test 1 "); my $body=$mw->Frame(-background=>'cyan')->pack(side=>'bottom', -fill=> +'x'); my $fr1=$body->Frame(-background=>'magenta')->pack(side=>'top', -fill= +>'x'); my $txt='hi'; $fr1->Label(-textvariable=>\$txt, -background=>'yellow', -foreground=> +'red')->pack(-fill=>'y'); sleep 5; change('foo', $fr1, $mw); sleep 5; change('bar', $fr1, $mw); MainLoop(); sub change { my ($new_txt, $fr1, $mw)=@_; $txt = $new_txt $fr1->pack(-fill=>'y'); $mw->update; }

        Again, I'm not a Tk guy, but I think the $txt variable gets tied to the label, so you just want to update the variable and get the label to refresh itself.

        Update: Just tested it here, and yup, it replaces the old label.

        Update 2: Fixed code formatting.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-03-28 17:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found