Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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!)

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

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.

share|improve this answer
    
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. –  Dillon Gilmore Jun 27 '12 at 8:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.