3

Is there a way to create a Gtk.Button("http://www.google.com"), when clicked will open the system default Web Browser using the label as the URL?

What I could do is...

import os
button = Gtk.Button(label="http://www.google.com")
label = button.get_text()
os.system("sensible-browser " + label)

The above code does what I need it to do. Is this the correct way about doing this? Or is there a GTK function I should be using instead?

(Disclaimer: I am usually more specific with my questions, I feel like this is really basic and may not even be possible and I have checked the docs. Thank you!)

2 Answers 2

5

Two solutions in one! :)

First, there is a special button widget just for this job: GtkLinkButton. You create a GtkLinkButton just like a normal button. Here is a simple example program written with Python and PyGObject. PyGTK should look pretty similar:

from gi.repository import Gtk
window = Gtk.Window()
button = Gtk.LinkButton("http://www.google.com", label="google.com")
window.add(button)
window.show_all()
Gtk.main()

As an alternative, you can directly call the function gtk_show_uri.

If you need more sophisticated application launching stuff, take a look at Gio.

1
  • Wow, I was editing my question and you had already posted. Thanks again I'll try gtk_show_uri first. I forgot to mention its a toolbarbutton, but I'll go look and see if a LinkButton exists for the toolbar.
    – iridian
    Jun 27, 2012 at 8:24
4

If someone else is coming here to know how to open a URI in the default browser without using a Gtk.LinkButton, the answer is using Gtk.show_uri_on_window():

Gtk.show_uri_on_window(None, "http://www.google.com", Gdk.CURRENT_TIME)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .