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

Perl Gtk2 AboutDialog's "set_url_hook()" is Not Working as Expected.

by mmartin (Monk)
on Aug 22, 2013 at 17:58 UTC ( #1050550=perlquestion: print w/ replies, xml ) Need Help??
mmartin has asked for the wisdom of the Perl Monks concerning the following question:

Hey Monks!

So I'm creating the AboutDialog for my GUI and I'm doing the "set_website" portion and trying to make the URL I enter actually do something when you click on it. I read that you do the set_url_hook() part before you run the set_website() method/function.

So I was having trouble just getting Perl to open a Browser and go to a URL, but I figured out how to do that part by using the Perl Module use Browser::Open qw( open_browser ); which seems to work pretty well. I then tried the code below and now, when I open the AboutDialog box, it instantly opens whatever URL I put in the "open_browser()" function from that Module without clicking on the link.
# Create a MessageDialog to display the "About" Info in: my $about_window = Gtk2::AboutDialog->new; $about_window->set_program_name("Program-Name"); $about_window->set_version('v1.0'); $about_window->set_copyright('August 2013'); $about_window->set_comments("This is the Comment String"); # Using Google.com just as an example: my $url = "http://www.google.com"; $about_window->set_url_hook(open_browser($url)); $about_window->set_website($url); $about_window->set_authors("....author_data...."); my $about_img = "$icon_DIR/Icon_Logo.png"; my $about_pixbuf = Gtk2::Gdk::Pixbuf->new_from_file($about_img); $about_window->set_logo($about_pixbuf); # Add the Hide action to the 'Close' button in the AboutDialog(): $about_window->signal_connect('response' => sub { $about_window->h +ide; }); $about_window->show;
So from what's going on here I would assume that whatever you put inside the "set_url_hook()" function gets executed immediately upon creation of the AboutDialog. I did see a SIGNAL available for the AboutDialog that's called "activate-link" but I couldn't connect it with "signal_connect" function...

Does anyone know what I'm missing here? I would assume there is something that I need to add to this to get it working?


UPDATE:
I've also noticed this... So I click the MenuItem to open the AboutDialog, and like I mentioned before, the set_url_hook() gets executed immediately without having to click the link. So I close the page that opened and go back to the About Window and click on the Link itself. When that happens I can see this error below being printed to the terminal.
Undefined subroutine &main::0 called at
And the line that the error references to is the line that has Gtk2->main; on it... So I tried putting a test sub in the set_url_hook() and I get the same error and whatever the Sub returns is what Perl is saying is a Undefined Subroutine.
So if my function returns "Hello", the Error would be:
Undefined subroutine &main::Hello called at
So I just thought I should mention that since I'm tinkering with it a bit...

Thanks in Advance,
Matt


Comment on Perl Gtk2 AboutDialog's "set_url_hook()" is Not Working as Expected.
Select or Download Code
Re: Perl Gtk2 AboutDialog's "set_url_hook()" is Not Working as Expected.
by Crackers2 (Parson) on Aug 22, 2013 at 19:46 UTC

    When you say this:

    $about_window->set_url_hook(open_browser($url));

    You're calling set_url_hook with as parameter the result of open_browser($url). I'm not familiar with Gtk2, but you probably want something like

    $about_window->set_url_hook("open_browser",$url);

    i.e. you tell set_url_hook the name of the function to call; it's possible that you have to pass the argument to open_browser in a different way; you'll have to experiment or read the docs to find out.

      Hey Crackers, thanks for the reply

      Humm, just tried what you showed and this time nothing happened when I opened the About window, which is good.

      But this time something actually did happen when I clicked on the link. I got an error from the open_browser() function but still good news because its actually now executing what's in there when I click the link... Thanks!

      I was about to walk out the door before you replied so I'll mess with it a bit tomorrow when I get back in.


      Thanks AGAIN for the reply, much appreciated!


      Thanks Again,
      Matt


      Ok... Got it working!

      So this is what I've found out this morning testing the set_url_hook() function. Seems that whatever is set in "$about_window->set_website($url);" for the $url variable will get passed to the "open_url()" sub which is an argument to the "$about_window->set_url_hook("open_url");" function, which is a subroutine.

      So with these 2 commands I can now successfully open the URL in a browser when I click on the link:
      ### 1st we need to define HOOK before we set the website: # *open_url() is a Subroutine: $about_window->set_url_hook("open_url"); ### Now we call the set_website() function: # *The $website var will be passed as an ARG to open_url(): $about_window->set_website($website); # This receives 2 Args: # ARG[0] --> AboutDialog Widget # ARG[1] --> $website sub open_url() { my ($widget, $url) = @_; print " url == $url\n"; print "Widget == $widget\n\n"; open_browser($url); }
      So now it seems everything is working as expected...! Thanks again Crackers, if you hadn't posted your example of the set_url_hook("open_broswer", $url) it probably would have taken me much longer to find that's how the syntax should be for that set_url_hook() Function's Arguments...!


      So thanks again for the reply, much appreciated!


      Thanks Again,
      Matt

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2015-07-05 18:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls