Skip to content

Instantly share code, notes, and snippets.

@RandomResourceWeb
Created July 17, 2016 08:57
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RandomResourceWeb/93e887facdb98937ab5d260d1a0df270 to your computer and use it in GitHub Desktop.
Save RandomResourceWeb/93e887facdb98937ab5d260d1a0df270 to your computer and use it in GitHub Desktop.
How to open a link through a tkinter button in Python
#---------------------------------
# Random Resource Web ©
# Randomresourceweb.blogspot.com
#---------------------------------
from tkinter import *
import webbrowser
root = Tk()
new = 1
url = "https://www.google.com"
def openweb():
webbrowser.open(url,new=new)
Btn = Button(root, text = "This opens Google",command=openweb)
Btn.pack()
root.mainloop()
@ctkqiang
Copy link

It appears to be blank tho.

@RandomResourceWeb
Copy link
Author

It appears to be blank tho.

Hey, I just re-ran this code and it should definitely be working!

@suchethg
Copy link

its working

@Staubgeborener
Copy link

Indeed, its working with Python3 and also with from Tkinter import * at line 6 wih Python2. Maybe you have some utf8 coding problems with that copyright sign.

@trashgaming325
Copy link

is there a way to put a box to type a link I want it to go too

@RandomResourceWeb
Copy link
Author

RandomResourceWeb commented Jan 26, 2020

is there a way to put a box to type a link I want it to go too

Hi, yea

  1. Add this piece of code after the openweb function (All it does it add an entry widget so you can enter a line of text)
    link = Entry(root)
    link.pack()
  2. And then replace url in the openweb function to link.get() (Which gets the value of the entry widget instead of the hard-coded url)
    Hope this helped!

@TheTechRobo
Copy link

This is awesome! Thanks! You got a star.

@KowshalTurna
Copy link

Thank you <3

@Ad0rableTrooper
Copy link

This is what I wanted. Thanks mate....

@NicoJqs
Copy link

NicoJqs commented May 31, 2021

Me sirvió para mi proyecto muchas gracias!

@alvarule
Copy link

Thank you !

@greeenboi
Copy link

can you guys try the same with custom tkinter? it doesn't seem to work as i intended

@ldrak20
Copy link

ldrak20 commented May 22, 2023

is there a way to add this with radio buttons, like if optionA is selected it opens linkA... ect ect?

@TheTechRobo
Copy link

@ldrak20 Well yes, the important part is the use of webbrowser.open function. You can call it from wherever you'd like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment