Skip to content

Gtk.LinkButton

class — extends Button, Accessible, Actionable, Buildable, ConstraintTarget

A button with a hyperlink.

<picture> <source srcset="link-button-dark.png" media="(prefers-color-scheme: dark)"> <img alt="An example GtkLinkButton" src="link-button.png"> </picture>

It is useful to show quick links to resources.

A link button is created by calling either LinkButton.new or LinkButton.new_with_label. If using the former, the URI you pass to the constructor is used as a label for the widget.

The URI bound to a GtkLinkButton can be set specifically using LinkButton.set_uri.

By default, GtkLinkButton calls FileLauncher.launch when the button is clicked. This behaviour can be overridden by connecting to the LinkButton.activate-link signal and returning True from the signal handler.

Shortcuts and Gestures

GtkLinkButton supports the following keyboard shortcuts:

  • <kbd>Shift</kbd>+<kbd>F10</kbd> or <kbd>Menu</kbd> opens the context menu.

Actions

GtkLinkButton defines a set of built-in actions:

  • clipboard.copy copies the url to the clipboard.
  • menu.popup opens the context menu.

CSS nodes

GtkLinkButton has a single CSS node with name button. To differentiate it from a plain GtkButton, it gets the .link style class.

Accessibility

GtkLinkButton uses the AccessibleRole.link role.

Constructors

new

@classmethod
def new(cls, uri: str) -> Widget

Creates a new GtkLinkButton with the URI as its text.

Parameters:

  • uri — a valid URI

new_with_label

@classmethod
def new_with_label(cls, uri: str, label: str | None = ...) -> Widget

Creates a new GtkLinkButton containing a label.

Parameters:

  • uri — a valid URI
  • label — the text of the button

Methods

get_uri

def get_uri(self) -> str

Retrieves the URI of the GtkLinkButton.

get_visited

def get_visited(self) -> bool

Retrieves the “visited” state of the GtkLinkButton.

The button becomes visited when it is clicked. If the URI is changed on the button, the “visited” state is unset again.

The state may also be changed using LinkButton.set_visited.

set_uri

def set_uri(self, uri: str) -> None

Sets uri as the URI where the GtkLinkButton points.

As a side-effect this unsets the “visited” state of the button.

Parameters:

  • uri — a valid URI

set_visited

def set_visited(self, visited: bool) -> None

Sets the “visited” state of the GtkLinkButton.

See LinkButton.get_visited for more details.

Parameters:

  • visited — the new “visited” state

Properties

uri

uri: str  # read/write

The URI bound to this button.

visited

visited: bool  # read/write

The 'visited' state of this button.

A visited link is drawn in a different color.

Signals

def on_activate_link(self) -> bool: ...

Emitted each time the GtkLinkButton is clicked.

The default handler will call FileLauncher.launch with the URI stored inside the LinkButton.uri property.

To override the default behavior, you can connect to the ::activate-link signal and stop the propagation of the signal by returning True from your handler.