Home > OS >  How to make a column of a table a hyperlink in python?
How to make a column of a table a hyperlink in python?

Time:04-19

Im building an alert system for my work that will automatically email out a table with data to the recipient. I iterate through the data of a SQL statement to fill a HTML table I've built. Everything works as is, however I would like the data of the first column to be a clickable hyperlink. Im not sure how to build the html in python, I need each cell in the row to be its own unique hyperlink.

Image of code

CodePudding user response:

You can try to change the second loop by adding this line:

for row in alertq:
    htmlString = htmlString   '<tr>'

    row[0] = f'<a href="{str(row[0]).strip()}">url</a>' # add this

    for col in row:
        htmlString = htmlString   '<td>'   str(col).strip()   '</td>'

    htmlString = htmlString   '</tr>'

I'm not a python pro, but it should work.

In this line I take what's in the first column, bring it to a string like each column, then wrap the content (as I understood from your words it should be a link) in a tag a, assigning the content of the first column (the link itself) to the a attribute of the tag a - href.

What I've specified inside the a tag itself (in this case text "url") will appear as active text so that when a user clicks on it they will follow the link specified in the href attribute.

You can specify any text inside a tag a, either as a single text for all columns or you can include it dynamically by setting a variable, wrapping it in curly braces as I did in the example.

CodePudding user response:

You can create a hyperlink on a label like this.

import tkinter as tk
import webbrowser


root = tk.Tk()
root.geometry("500x500")
root.title("Hyperlink")

def hyperlink(url):
   webbrowser.open_new_tab(url)


website_link = tk.Label(root, text="www.stackoverflow.com",font="times 14 underline", fg="blue", cursor="hand2")
website_link.pack()
website_link.bind("<Button-1>", lambda x: hyperlink("http://www.stackoverflow.com"))

root.mainloop()
  • Related