python Gui... How to create a button which goes to a web page?

0 Mertcan TOK · April 15, 2015
 i was watching the tutorials about python GUI and wondered whether we can create a button which opens default web browser and goes a pre defined web page or not.
Such as clickbutton => open Mozilla °> go https://www.thenewboston.com
Thank you

Post a Reply

Replies

Oldest  Newest  Rating
+3 alex constantin · April 15, 2015
You will have to import the webbrowser module like so and create a callback functions like this:

import webbrowser

url = 'https://thenewboston.com'

def openUrl():
   webbrowser.open(url, 2) # The second parameter is todo with proxies and # other features. Number 2 is a normal open

myButton = Button(root, text='Open URL', command=openUrl)
myButton.pack()


Something like that should work. I'm pretty sure Python comes with the webbrowser module, if not, you will have to download it, but like I say, I am pretty sure it already comes with Python

Good luck
0 Mertcan TOK · April 16, 2015
i couldnt upload any image files but here is the code and it worked. Thanks for the help.

from tkinter import *
import webbrowser

url = 'https://thenewboston.com'

def openUrl():
webbrowser.open(url,2)

root = Tk()
myButton = Button(root, text='go to the new boston', command= openUrl)
myButton.pack()
root.mainloop()
0 alex constantin · April 16, 2015
No problem, glad I helped! :)
  • 1

Python

107,052 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator