Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Perl TK

by randor (Novice)
on Sep 21, 2012 at 15:08 UTC ( #994921=perlquestion: print w/replies, xml ) Need Help??
randor has asked for the wisdom of the Perl Monks concerning the following question:


Ok, I have decided to take on Perl Tk as a project, basically because I love Perl and have always wanted to make a stand alone GUI.

I have found most of what I need except I have run into seemingly 2 problems

1: How to create a link in the GUI to open a website with a browser? I need to be able to put in a spot where the user can click and it will open a web browser with a certain page on it.

2: Is there a way to print something in the GUI??

Thank you for any help.

PS: I know Tk is no longer active but I do like its easability.

Replies are listed 'Best First'.
Re: Perl TK
by zentara (Archbishop) on Sep 21, 2012 at 17:35 UTC
    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 = ''; #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 => '', -tags => ['weblink'] ); $c->bind('weblink', '<ButtonPress-1>', sub { print "launch your url\n"; my $linkurl = ""; 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
      Thank you but I tried your code by copy and pasting it so I could play with it and I got an error saying "firefox is not a recognizable command"

      any Ideas?

        Well, you could install firefox or use a sensible method to launch the default browser with the URL. For Windows:


        For Linux:


        And so on.

Re: Perl TK
by Rudolf (Pilgrim) on Sep 21, 2012 at 16:24 UTC

    Hello randor, I have made one or two GUI's using Tk and the easiest way to print something to it is using a label:

    $mainWindow-> Label(-text => "Hello world");

    As far as posting a link, I think you might have to make a button which the user presses and callback to a function which takes you to that webpage. Your going to need to know which web browser to use and what parameters to pass it.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://994921]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2017-05-28 18:04 GMT
Find Nodes?
    Voting Booth?