NAME

Tk::HyperlinkButton - Create a clickable hyperlink button to open a web browser

SYNOPSIS

use Tk::HyperlinkButton;

my $mw = MainWindow->new();

my $link_text = 'metacpan.org';
my $link_target = 'http://www.metacpan.org';
my $link_callback = sub{ print "your callback here\n"; };

my $hyperlink_widget = $mw->HyperlinkButton(
    -text => $link_text,
    -target => $link_target,
    -command => $link_callback,
);

$hyperlink_widget->pack;

$mw->MainLoop;

DESCRIPTION

Tk::Hyperlink is an adjusted Tk::Button widget to display a hyperlink. The hyperlink will be displayed with blue foreground color by default and highlighted on mouse over. The look and feel resembles a Label (no borders by default).

A custom callback can be provided the same way as for other buttons (using the option -command). The callback will be executed when the hyperlink is clicked. The default callback will open the system's default web browser and navigate to the URL provided by the option target. If target is not defined, the URL will be what you define by the option -text.

The widget is indented to only represent the hyperlink. The intended use case is actually to have some kind of Tk::Label-like widget that is a clickable hyperlink.

Text in front or after the hyperlink should be created with other means (e.g. Tk::Label or Tk::Text).

WIDGET-SPECIFIC OPTIONS

target

Sets the address for the hyperlink. If target is not given, -text will be used as target automatically.

Using -text and -target in combination, you can get a clickable hyperlink that open a web browser with the address defined in -target, but that displays a different text as defined in -text. Basically, that's what you have in HTML as well.

command

A callback that will be executed when the hyperlink is clicked. Optional. By default, clicking the hyperlink will open the system's default browser and navigate to the URL (cf. -target).

WIDGET METHODS

The HyperlinkButton method creates a widget object. This object supports the configure and cget methods described in Tk::options which can be used to enquire and modify the options described above. The widget also inherits all the methods provided by the generic Tk::Widget class.

Open a browser and navigate to the hyperlink target (cf. -target). Uses Browser::Open for starting the browser.

BINDINGS

When a new hyperlink is created, it has default instance event bindings to the following events:

  • Any-Enter: Will highlight the hyperlink similar to what you see in a web browser. Also changes the cursor.

  • Any-Leave: Will reset the highlightning or appearance changes done by the other events.

This widget is intended to be interactive.

TODO

* Describe how to embed hyperlinks in Tk::Text / Tk::ROText.
* configuration options for font and colors

SEE ALSO

Tk::Button, Browser::Open

Code is inspired by https://www.perlmonks.org/?node_id=667664, downloaded 2019-11-01.

AUTHOR

Alexander Becker, <asb@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2019 by Alexander Becker

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.28.0 or, at your option, any later version of Perl 5 you may have available.

KEYWORDS

button, widget, hyperlink, link, web link