Home > Software engineering >  Adding a hyperlink to Tkinter Treeview Values
Adding a hyperlink to Tkinter Treeview Values

Time:11-25

I'm putting together a decision tree tool using Tkinter. I would like to turn the values in the hyperlink column into clickable hyperlinks. How do i do this?

enter image description here

Here is the relevant code.

root=Tk()
root.title('Decision Tree')
root.geometry("600x600")

my_tree = ttk.Treeview(root)

#Define the columns
my_tree['columns'] = ("Decision", "Hyperlinks", "ID")

#format the columns
my_tree.column("#0", width=250, minwidth=100)
my_tree.column("#1", width=0, stretch="No")
my_tree.column("Hyperlinks", anchor=W, width=200)
my_tree.column("ID", anchor=CENTER, width=80)

#Create Headings
my_tree.heading("#0", text="Decision", anchor=W)
my_tree.heading("#1", text="", anchor=W)
my_tree.heading("Hyperlinks", text="Hyperlinks", anchor=W)
my_tree.heading("ID", text="ID", anchor=CENTER)

#Add Data 
my_tree.insert(parent='', index='1', iid=0, text="Problem 1", values=("", "", "1"))
my_tree.insert(parent='', index='1', iid=2, text="Problem 2", values=("", "", "3"))
my_tree.insert(parent='', index='1', iid=1, text="Problem 3", values=("", "", "2"))

#Add child level 1
my_tree.insert(parent='0', index='end', iid=6, text="Prob 1 level 2", values=("", "Hyperlink 1", "1.1"))
my_tree.insert(parent='1', index='end', iid=7, text="Prob 3 level 2", values=("", "Hyperlink 2", "3.1"))
my_tree.insert(parent='2', index='end', iid=8, text="Prob 2 level 2", values=("", "Hyperlink 2", "2.1"))

#Add child level 2
my_tree.insert(parent='6', index='end', iid=9, text="Prob 1 level 3", values=("", "", "1.11"))
my_tree.insert(parent='7', index='end', iid=10, text="Prob 2 level 3", values=("", "", "2.21"))

my_tree.pack(pady=20)
root.mainloop()

CodePudding user response:

It should be fairly straightforward to grab the hyperlink from the treeview selection and open it in a browser

import webbrowser as wb


def open_link(event):
    tree = event.widget  # get the treeview widget
    item = tree.item(tree.focus())  # get the treeview selection
    link = item['values'][1]  # get the link from the selected row
    wb.open_new_tab(link)  # open the link in a browser tab


# bind the selection event to 'open_link'
my_tree.bind('<<TreeviewSelect>>', open_link)  

Note that this will trigger when you select an item from the treeview, i.e when you click on a row of the table., rather than clicking specifically on a hyperlink in the 2nd column. If you want to do that, you have to be more particular...

import webbrowser as wb


def open_link(event):
    tree = event.widget  # get the treeview widget
    region = tree.identify_region(event.x, event.y)
    col = tree.identify_column(event.x)
    iid = tree.identify('item', event.x, event.y)
    if region == 'cell' and col == '#2':
        link = tree.item(iid)['values'][1]  # get the link from the selected row
        wb.open_new_tab(link)  # open the link in a browser tab


# bind left-click to 'open_link'
my_tree.bind('<Button-1>', open_link)

Now the link should only open when the user clicks on a link in the "Hyperlinks" column

  • Related