Friday, May 10, 2024
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 2618  / 3 Years ago, thu, july 22, 2021, 1:55:20

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


More From » application-development

 Answers
2

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.


[#37249] Friday, July 23, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ravturtl

Total Points: 335
Total Questions: 132
Total Answers: 110

Location: Tanzania
Member since Wed, Feb 24, 2021
3 Years ago
;