How to make the link clickable, to be accessed?

Asked

Viewed 5,061 times

0

print('-----')
print('SITES')
print('-----')

print()
print()




print('Duolingo Brasileiro')
print('Duolingo Inglês')
print('Duolingo Alemão')

escolha_site = str(input('Escolha qual versão do site do duolingo deseja acessar: '))


if escolha_site == 'Duolingo Brasileiro':
print('https://pt.duolingo.com/')

elif escolha_site == 'Duolingo Inglês':
print('https://en.duolingo.com/')

elif escolha_site == 'Duolingo Alemão':
print('https://de.duolingo.com/')

What I want to do is that when the user wishes to access such site, after he chose, he could click on the link, and would be redirected to the default PC browser, in my case is Chrome. I’m not an advanced/intermediate python user, but I already see the need to learn a little about how to work the web with python. Remembering that these "duolingos", are only for examples, I could have used other sites as example. Sorry for any formatting error! I’m still new here and I have a lot of work to put the code, I have to keep giving spaces... anyway, I don’t know how to move much here. I used "print" only to give an even interpretation, because, I do not know with which command or module I will need to use to make the link clickable..

  • This program is running on the same terminal?

  • No, I’m running on python’s own IDLE. I want to make the link clickable, remembering that PRINT is just an example, I do not know which command turns the link clickable, to run in the browser.

1 answer

1


Brkappa,

Try following the example below, simply using an HTML link:

print('<a href="http://www.exemplo.com.br">Texto do link</a>')

To open an external link in the browser, you must use the webbrowser module:

import webbrowser

webbrowser.open('https://pt.duolingo.com/')

It is possible to use buttons and do something like this:

from Tkinter import *

app = Tk()

frame = Frame(app)
frame.pack()

url = "https://pt.duolingo.com/"

def OpenUrl():
   webbrowser.open_new(url)

button = Button(frame, text="Duolingo PT", command=lambda aurl=url:OpenUrl(aurl))
button.pack(side = "left", padx = 20, pady = 20)

app.mainloop()

The webbrowser module has the following functions:

webbrowser.open(url, new=0, autoraise=True)

Displays the URL using the default browser. If new is 0 the URL will open in the same browser window if possible. If new is 1, a new browser window will open if possible. If new is 2, a new page (tab) of the browser will open if possible. If autoraise is True, the window will be enlarged if possible.

webbrowser.open_new(url)

Open the URL in a new window of the default browser, if possible, otherwise open the URL in the single browser window.

webbrowser.open_new_tab(url)

Open the URL on a new page (tab) of the default browser, if possible, else does the same as open_new()

webbrowser.get([name])

Returns a control object to the browser type. If the name is empty, returns an appropriate controller for a default environment browser.

webbrowser.register(name, constructor[, instance])**

Records the name of the browser type. Once a browser type is registered, the function get() can return a controller to this type of browser. If the instance is not provided, or is None, the constructor will be called without parameters to create an instance when needed. If the instance is provided, the constructor will never be called and can be None.

This entry point is only useful if you plan to set the BROWSER variable or call get() with a non-empty argument corresponding to the name of a handler you declare.

The webbrowser module has its documentation on the link:

  • I want to make the link be clickable, summarizing, I want python to show the link and I can open this link in the browser, understood?

  • Through the console?

  • In the IDLE itself, not in the python terminal (if you have how, then I want yes).

  • Have you tested the example of the answer? What happened?

  • He only printed that ('<a href="https://pt.duolingo.com">Duolingo Brasileiro</a>'), remembering, I want the link to be clickable, I want to be able to click the link to go to the browser...

  • @Brkappa adjusted the answer, check if you can...

  • Thanks, it worked! You already knew that or had to research??

  • A little thing, how can I see the other commands that this module has? I want to work a lot using web.

  • I had already used this module in test projects, but I had to research to remember how I did it. I put more information on the webbrowser module.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.