Skip to content

Instantly share code, notes, and snippets.

@RandomResourceWeb
Created July 17, 2016 08:57
Show Gist options
  • Select an option

  • Save RandomResourceWeb/93e887facdb98937ab5d260d1a0df270 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

It appears to be blank tho.

@RandomResourceWeb

Copy link
Copy Markdown
Author

It appears to be blank tho.

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

@suchethg

Copy link
Copy Markdown

its working

@Staubgeborener

Copy link
Copy Markdown

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
Copy Markdown

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

@RandomResourceWeb

RandomResourceWeb commented Jan 26, 2020

Copy link
Copy Markdown
Author

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
Copy Markdown

This is awesome! Thanks! You got a star.

@KowshalTurna

Copy link
Copy Markdown

Thank you <3

@Ad0rableTrooper

Copy link
Copy Markdown

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

@NicoJqs

NicoJqs commented May 31, 2021

Copy link
Copy Markdown

Me sirvió para mi proyecto muchas gracias!

@alvarule

Copy link
Copy Markdown

Thank you !

@greeenboi

Copy link
Copy Markdown

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

@ldrak20

ldrak20 commented May 22, 2023

Copy link
Copy Markdown

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

@TheTechRobo

Copy link
Copy Markdown

@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