Home > Blockchain >  How can I Print a clickable text in python, which go to a URL
How can I Print a clickable text in python, which go to a URL

Time:10-31

I would like to print a text using python [ on pycharm terminal ] with a URL. Can any one suggest how can I do that.

`url = https://stackoverflow.com/.com
print ("Click me")`

I am expecting the text "Click me" to be printed as a clickable text, which can go to https://stackoverflow.com/.com

CodePudding user response:

There are two possibilites to open a hyperlink.

1. Open with the standard terminal:

Here you can not influence the click feature by Python code. To open a link in the Terminal just print() the link with Python to the Terminal with Stdout and use Ctrl Left-Click to open the link.

print("https://www.stackoverflow.com")

2. Use a GUI Framework to design a click button:

This is the recommend way to design such a click feature, because of the better ergonomics.

To design a simple GUI, I recommend using tkinter, especially in the beginning.

You can then simply implement your button and make it clickable by applying an event to it. To open the link you can simply use the webbrowser module and call the webbrowser.open() function.

from tkinter import *
import webbrowser

window = Tk()

def onClick():
    webbrowser.open("https://www.stackoverflow.com") # Opening the URL

button = Button(window, text="Click me!", command=onClick) # Assigning button and adding the click event.
button.pack()

window.mainloop()

CodePudding user response:

Replace this:

.com/.com

to:

.com/

Does this will help?

import webbrowser
from tkinter import *

root = Tk()
root.title("WebBrowsers")

root.geometry("640x480")

webbrowser.open("https://stackoverflow.com/")
  • Related