Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I know Tk is no longer active but I do like its easability.

Tk is still active, it just dosn't change much. :-)

What you are looking for is Tk's hypertext. See Insert something like a hyperlink in a Tk Text widget for a few basic examples.

To launch the web browser when clicking on a hypertext link, use the code below in the callback. You need to fork it off to avoid blocking the event loop.

#!/usr/bin/perl use warnings; use strict; my $linkurl = 'http://google.com'; #my $linkurl = 'linux-tips.html'; #my $file = 'links_from_HTML.html'; #my $command = "firefox $file"; # if(fork() == 0){ exec ($command) } #works #system( $command ); #external url my $command = "firefox $linkurl"; if(fork() == 0){ exec ($command) }
If you don't want to use HyperText, you can put the callback into a Button widget.

You can also do it very nicely on a canvas, the canvas allows you much flexibility. The following example could be enhanced, to have Enter and Leave bindings on the weblink tag, and to maybe change the cursor to a hand or pointer when over the links.

#!/usr/bin/perl use Tk; use strict; my $w=20; my $x=0; my $y=0; my %nums = ( 0 => ['black','yellow'], 1 => ['yellow','black'], 2 => ['white','green'], 3 => ['green','white'], 4 => ['grey','red'], 5 => ['red','grey'], 6 => ['blue','white'], 7 => ['white','blue'], 8 => ['orange','grey45'], 9 => ['grey45','orange'], ); my $mw=tkinit; my $c = $mw->Canvas(-bg=>'white')->pack; for (0..9) { my $item=$c->createRectangle($x,$y,$x+20,$y+20, -fill=> ${$nums{$_}}[0], -tags => ['weblink'] ); my $text = $c->createText($x+10,$y+10, -anchor=>'center', -fill => ${$nums{$_}}[1], -text => $_, -tags => ['weblink'] ); $x+=20; } my $text1 = $c->createText(100,100, -anchor=>'center', -fill => 'black', -font => 24, -text => 'http://google.com', -tags => ['weblink'] ); $c->bind('weblink', '<ButtonPress-1>', sub { print "launch your url\n"; my $linkurl = "http://google.com"; my $command = "firefox $linkurl"; if(fork() == 0){ exec ($command) } } ); $mw->Button( -text => "Exit", -command =>sub{ exit }, )->pack; MainLoop;

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

In reply to Re: Perl TK by zentara
in thread Perl TK by randor

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-18 06:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found