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

Fellow monks,

I am in the process of writing a Perl/Tk program to watch stocks during the day while I'm at work. I used to use a free application to do this, but it's suddenly failed, and the author's web site is unreachable, so I'm trying to recreate his program in Perl.

All is well and good, but I'm stuck on how to do a hyperlink in Tk. I've looked on the web, in Mastering Perl/Tk and in Super Search and come up empty.

The closest I've found is the psuedo code from the Mastering book and it's not helping. I understand the bit

$t->tagBind('goto_stock',"<Button-1>", sub{ ?? });

but I'm stuck on what exactly to call in the subroutine. I know I could just do `iexplorer` or `mozilla` but that doesn't seem right and I don't want to hard code a browser in to the program.

How would Tk know that I want to bind to a hyperlink? Is there a generic way to determine what the default browser on the system is?

Also if there's an example of this somewhere that I've missed, I'd really appreciate it if someone could let me know.

Update: Sorry I didn't make it clear initially, but I want to be able to use the binding to a hyperlink to visit Yahoo Finance to get to the page for the stock in question - e.g. GE, not to watch the quotes in a browser window. The quotes are being updated from a CSV file that I'm grabbing from Yahoo.

There is no emoticon for what I'm feeling now.

Replies are listed 'Best First'.
Re: Binding hyperlinks to text using Perl/Tk
by kvale (Monsignor) on Mar 11, 2004 at 06:18 UTC
    There might be some confusion on the two sorts of hyperlinks. A hyperlink in Tk has nothing do do with hyperlinks in a browser. The only thing they share in common is that something happens when you click on a piece of text (or picture).

    tagBind knows nothing of the web; it only allows you to bind some action with clicking on the text. That action is up to you. Typically, tagBind is used to link different parts of a long text, as in a table of contents. But you could certainly use it to open a page on a browser.

    There are a couple of ways to do that. One is to simply invoke the browser with a system statement. Another is to communicate with the browser using some IPC like Applescript on the Mac of OLE on Windows.

    -Mark

      Thanks for that! That was sort of my understanding, so I think, if I understand bind correctly, I can just use bind, not necessary tagBind and I should be okay.

      So I guess my question boils down to finding the default browser on a system. Any ideas on that? :)

      There is no emoticon for what I'm feeling now.

        On windows start http://url/ will launch the default web browser.
Re: Binding hyperlinks to text using Perl/Tk
by saintmike (Vicar) on Mar 11, 2004 at 05:43 UTC
    Not sure why you would open a browser to watch stock quotes. Here's a thread on Perl Monks on how to retrieve data from the web and populate a GUI with it.

    And this response points to an article where you can simply download a script that's ready for you to use ... :)

    -- saintmike

      Sorry, I guess I didn't make that quite clear. My program is going to update every x seconds, but I want to have the bind option to go to the Yahoo page of the stock itself. I never personally used that option, but I believe that Yahoo has links to news for the company if you went to Yahoo Finance and looked for GE for example.

      And I do remember looking at that discussion when it appeared, but I'm sort of cheating. Yahoo has an option to download a CSV of a list of stocks you pick, so I'm grabbing that and then parsing out what I want from there. I'm not worried about things hanging because I'm doing the rewrites myself after fetching the CSV.

      There is no emoticon for what I'm feeling now.

Re: Binding hyperlinks to text using Perl/Tk
by zentara (Cardinal) on Mar 11, 2004 at 15:28 UTC
    This might give you some ideas.
    #!/usr/bin/perl use Tk; $mw = MainWindow->new( -title => "hyperlinks" ); $t = $mw->Scrolled('Text')->pack; $tag = "tag000"; foreach (<DATA>) { chomp; split (/(http:\S+)/); foreach (@_) { if (/(http:\S+)/) { $t->insert( 'end', $_, $tag ); $t->tagConfigure( $tag, -foreground => 'blue' ); $t->tagBind( $tag, '<Any-Enter>' => [ \&manipulate_link, $tag, 'raised', +'hand2' ] ); $t->tagBind( $tag, '<Any-Leave>' => [ \&manipulate_link, $tag, 'flat', 'x +term' ] ); $t->tagBind( $tag, '<Button-1>' => [ \&manipulate_link, $tag, 'sunken' ] +); $t->tagBind( $tag, '<ButtonRelease-1>' => [ \&manipulate_link, $tag, 'raised', undef, \&printm +e ] ); $tag++; } else { $t->insert( 'end', $_ ); } } $t->insert( 'end', "\n" ); } MainLoop; sub printme { local ($,) = " "; print "printme:", @_, "\n"; } sub manipulate_link { # manipulate the link as you press the mouse key my ($a) = shift; my ($tag) = shift; my ($relief) = shift; my ($cursor) = shift; my ($after) = shift; # by configuring the relief (to simulate a button press) $a->tagConfigure( $tag, -relief => $relief, -borderwidth => 1 ); # by changing the cursor between hand and xterm $a->configure( -cursor => $cursor ) if ($cursor); # and by scheduling the specified action to run "soon" if ($after) { my ($s) = $a->get( $a->tagRanges($tag) ); $main::mw->after( 200, [ $after, $a, $s, $tag, @_ ] ) if ($aft +er); } } __DATA__ Hi there. This is text. THis is more text but http://this.is.a/hyperlink in a line. http://this.is.another/hyperlink followed by http://this.is.a.third/hyperlink __END__
    As far as determining the default browser, I would look at how midnight commander launches html in it's mc.ext file. This works with Netscape and Mozilla
    # htm regex/\.(htm||HTM)$ Open=if test -n "gnome-moz-remote" && test -n "$DISPLAY"; then + (gnome-moz-remote file://%d/%p &) >/dev/null 2>&1; else links %f 2>/ +dev/null || lynx -force_html %f; fi View=%view{ascii} lynx -dump -force_html %f

    I'm not really a human, but I play one on earth. flash japh