0

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()

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

When i try the first option you suggested, it does work when i click the row that contains the hyperlink. What also happens though is when i click any other row it opens windows explorer to where the python file is stored. If I try the second (more specific) method I get "TypeError: string indices must be integers"
The "less picky" first option assumes that there will always be a hyperlink in the 2nd column of each item. For the second one, what do you see when you print(item['values']) after item = tree.identify('item', event.x, event.y)?
tree.identify("item",...) returns the item ID which is a string, so item["values"] will raise the exception.
@acw1668 Thanks for the help. Should be fixed now! OP, if you know that your 'hyperlinks' column might not always have content for each item, you can further filter by adding another if clause after link = tree.item(iid)['values'][1]. E.g.: if link: to treat any non-empty value as a hyper link, or if 'http' in link to look for web addresses...you get the idea!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.